file sharing in linux

S

smarty

can any one tell me what's wrong with this program?

#include<sys/stat.h>
#include<fcntl.h>
main()
{
int pid,fd,pd;
pid=fork();
char *data,*str="child writes to the file";
if(pid == 0)
{
printf("IN CHILD\tcreating a file to COMMUNICATE\n");
fd=open("sarma.c",O_CREAT|O_APPEND|O_RDWR);
perror("OPEN OPERATION");
write(fd, str , 50);
perror("WRITE OPERATION");
fsync(fd);
perror("FSYNC OPERATION");
close(fd);
printf("child bids farewell\n");
}
else
{
printf("IN PARENT\tSLEEPING................\n");
wait(0);
printf("IN PARENT\topening the file to COMMUNICATE\n");
pd=open("sarma.c",O_RDONLY);
perror("OPEN OPERATION");
read(pd,data,50);
perror("READ OPERATION");
close(pd);
printf("PARENT reads:\"%s\"\n",data);
printf("PARENT dies......\n");

}
}

The parent is reading the shared file but with an "illegal file
access" error.....
Check out the result on my system

IN PARENT SLEEPING................
IN CHILD creating a file to COMMUNICATE
OPEN OPERATION: Success
WRITE OPERATION: Illegal seek
FSYNC OPERATION: Illegal seek
child bids farewell
IN PARENT opening the file to COMMUNICATE
OPEN OPERATION: Success
READ OPERATION: Illegal seek
PARENT reads:"child writes to the file"
PARENT dies......
Segmentation fault


what is this segmentation fault for?
 
C

chander.kashyap

can any one tell me what's wrong with this program?

#include<sys/stat.h>
#include<fcntl.h>
main()
{
int pid,fd,pd;
pid=fork();
char *data,*str="child writes to the file";
if(pid == 0)
{
printf("IN CHILD\tcreating a file to COMMUNICATE\n");
fd=open("sarma.c",O_CREAT|O_APPEND|O_RDWR);
perror("OPEN OPERATION");
write(fd, str , 50);
perror("WRITE OPERATION");
fsync(fd);
perror("FSYNC OPERATION");
close(fd);
printf("child bids farewell\n");
}
else
{
printf("IN PARENT\tSLEEPING................\n");
wait(0);
printf("IN PARENT\topening the file to COMMUNICATE\n");
pd=open("sarma.c",O_RDONLY);
perror("OPEN OPERATION");
read(pd,data,50);
perror("READ OPERATION");
close(pd);
printf("PARENT reads:\"%s\"\n",data);
printf("PARENT dies......\n");

}

}

The parent is reading the shared file but with an "illegal file
access" error.....
Check out the result on my system

IN PARENT SLEEPING................
IN CHILD creating a file to COMMUNICATE
OPEN OPERATION: Success
WRITE OPERATION: Illegal seek
FSYNC OPERATION: Illegal seek
child bids farewell
IN PARENT opening the file to COMMUNICATE
OPEN OPERATION: Success
READ OPERATION: Illegal seek
PARENT reads:"child writes to the file"
PARENT dies......
Segmentation fault

what is this segmentation fault for?

Well Dear what is good in this program:
Issues are:

No error checking weather file is opened or not.
You are printing data which contains some garbage reference.
Please do proper error checking.
 
C

chander.kashyap

can any one tell me what's wrong with this program?

#include<sys/stat.h>
#include<fcntl.h>
main()
{
int pid,fd,pd;
pid=fork();
char *data,*str="child writes to the file";
if(pid == 0)
{
printf("IN CHILD\tcreating a file to COMMUNICATE\n");
fd=open("sarma.c",O_CREAT|O_APPEND|O_RDWR);
perror("OPEN OPERATION");
write(fd, str , 50);
perror("WRITE OPERATION");
fsync(fd);
perror("FSYNC OPERATION");
close(fd);
printf("child bids farewell\n");
}
else
{
printf("IN PARENT\tSLEEPING................\n");
wait(0);
printf("IN PARENT\topening the file to COMMUNICATE\n");
pd=open("sarma.c",O_RDONLY);
perror("OPEN OPERATION");
read(pd,data,50);
perror("READ OPERATION");
close(pd);
printf("PARENT reads:\"%s\"\n",data);
printf("PARENT dies......\n");

}

}

The parent is reading the shared file but with an "illegal file
access" error.....
Check out the result on my system

IN PARENT SLEEPING................
IN CHILD creating a file to COMMUNICATE
OPEN OPERATION: Success
WRITE OPERATION: Illegal seek
FSYNC OPERATION: Illegal seek
child bids farewell
IN PARENT opening the file to COMMUNICATE
OPEN OPERATION: Success
READ OPERATION: Illegal seek
PARENT reads:"child writes to the file"
PARENT dies......
Segmentation fault

what is this segmentation fault for?

And illegal file seek is an error not success. Its not reading file.
Read operation is failed.
 
S

Spiros Bousbouras

can any one tell me what's wrong with this program?

#include<sys/stat.h>
#include<fcntl.h>
main()
{
int pid,fd,pd;
pid=fork();
char *data,*str="child writes to the file";
if(pid == 0)
{
printf("IN CHILD\tcreating a file to COMMUNICATE\n");
fd=open("sarma.c",O_CREAT|O_APPEND|O_RDWR);
perror("OPEN OPERATION");
write(fd, str , 50);
perror("WRITE OPERATION");
fsync(fd);
perror("FSYNC OPERATION");
close(fd);
printf("child bids farewell\n");
}
else
{
printf("IN PARENT\tSLEEPING................\n");
wait(0);
printf("IN PARENT\topening the file to COMMUNICATE\n");
pd=open("sarma.c",O_RDONLY);
perror("OPEN OPERATION");
read(pd,data,50);
perror("READ OPERATION");
close(pd);
printf("PARENT reads:\"%s\"\n",data);
printf("PARENT dies......\n");

}

}

The parent is reading the shared file but with an "illegal file
access" error.....
Check out the result on my system

IN PARENT SLEEPING................
IN CHILD creating a file to COMMUNICATE
OPEN OPERATION: Success
WRITE OPERATION: Illegal seek
FSYNC OPERATION: Illegal seek
child bids farewell
IN PARENT opening the file to COMMUNICATE
OPEN OPERATION: Success
READ OPERATION: Illegal seek
PARENT reads:"child writes to the file"
PARENT dies......
Segmentation fault

what is this segmentation fault for?

The first thing wrong with this programme is that
it contains functions and headers which are not
part of standard C. It belongs at comp.unix.programmer
not comp.lang.c

But here are a couple of comments anyway:
You are calling perror() when you don't know
that there has actually been an error hence
the error message you're getting may well be
garbage.

As for the segmentation fault the pointer data
has not been initialised.
 
J

Jens Thoms Toerring

smarty said:
can any one tell me what's wrong with this program?

Several things already with your use of C. And then it
uses lots of system specific extensions which should be
discussed in a group that deals with programming for
POSIX systems like comp.unix.programmer. I will try to
concentrate on the C specific issues here (as far as that
can be untangled). Correct those and then ask for further
help in the above mentioned group.
#include<sys/stat.h>
#include<fcntl.h>

These are both non-standard C headers. While I
have no idea what you need <fcntl.h> for you
will need some more of these system specific
headers, e.g. <unistd.h>, <sys/wait.h> and
<sys/types.h>.

And you need a C standard header

#include <stdio.h>

for prototypes for printf() and perror().

Since main is always returning an int make that

int main( void )
{
int pid,fd,pd;
pid=fork();

A system specific function that also can fail, you should
check for that. And it returns someting of type 'pid_t'
which can but doesn't have to be an int...
char *data,*str="child writes to the file";
if(pid == 0)
{
printf("IN CHILD\tcreating a file to COMMUNICATE\n");
fd=open("sarma.c",O_CREAT|O_APPEND|O_RDWR);

A system specific function that can fail, you should
check for that.
perror("OPEN OPERATION");

This is no replacement for error checking since it only
prints out something but doesn't do anything to deal
with the error (i.e. if open() failed you probably
shouldn't trying to write() but instead bail out).
write(fd, str , 50);

Another system specific function. And here you make also a
bad mistake from a C point of view since you ask write()
to write more characters to the file then there are in
the string 'str' points to which entails that write()
will read past the end of that string, i.e. will
access memory that doesn't belong to your program.
perror("WRITE OPERATION");
fsync(fd);

One more system specific function (and probaby quite
unnecesarry).
perror("FSYNC OPERATION");
close(fd);

And one more of them...
printf("child bids farewell\n");
}
else
{
printf("IN PARENT\tSLEEPING................\n");
wait(0);

If you believe that this would ensure that the child is
running first you're mistaken. Hint: the childs PID isn't
0, it's stored in 'pid' (at least if fork() succeeded).
printf("IN PARENT\topening the file to COMMUNICATE\n");
pd=open("sarma.c",O_RDONLY);
Dito.

perror("OPEN OPERATION");
read(pd,data,50);

Now, assuming that everything went fine then here
comes the worst problem: 'data' isn't initialized,
it points to some random place in memory. And now
you ask read() to write over that memory. You have
to allocate an array of 50 characters first, make
'data; point to that (or define data as a char
array with 50 elements) before you can use it that
way. This may be the cause of the segmentation
fault you get later.
perror("READ OPERATION");
close(pd);
Dito.

printf("PARENT reads:\"%s\"\n",data);
printf("PARENT dies......\n");
}
}

main() is supposed to return an int, so you should
have something like

return 0;

before the end of main.
The parent is reading the shared file but with an "illegal file
access" error.....

First thing you should do before calling perror() is
test if there was an error at all. Once you have
done that and removed the other problems pointed
out ask in comp.unix.programmer for the system
specific problems with your program.
what is this segmentation fault for?

Once you wrote to memory that doesn't belong to your
program all bets are off. Perhaps you wrote over some
internal data that the program needs while shutting
down. Or something else...

Regards, Jens
 
K

Kenneth Brody

smarty said:
can any one tell me what's wrong with this program?
[...]
char *data,*str="child writes to the file"; [...]
read(pd,data,50);
[...]
Segmentation fault

what is this segmentation fault for?

Well, ignoring all the non-standard parts of the program, your
problem comes down to the above 2 lines. You have passed read()
an uninitialized pointer, causing the read (if it succeeds) to
overwrite who-knows-what.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Antoninus Twink

can any one tell me what's wrong with this program?

#include<sys/stat.h>
#include<fcntl.h>

Firstly, some missing headers (unistd.h and stdio.h).

Secondly, this should give you some hints of where to look for problems:

$ valgrind ./a >/dev/null
==14808== Memcheck, a memory error detector.
==14808== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et
al.
==14808== Using LibVEX rev 1804, a library for dynamic binary
translation.
==14808== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==14808== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation
framework.
==14808== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et
al.
==14808== For more details, rerun with: -v
==14808==
==14811== Syscall param open(mode) contains uninitialised byte(s)
==14811== at 0x40007F2: (within /lib/ld-2.7.so)
==14811== by 0x405F44F: (below main) (in /lib/i686/cmov/libc-2.7.so)
OPEN OPERATION: Inappropriate ioctl for device
WRITE OPERATION: Illegal seek
FSYNC OPERATION: Illegal seek
==14811==
==14811== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from
1)
==14811== malloc/free: in use at exit: 0 bytes in 0 blocks.
==14811== malloc/free: 3 allocs, 3 frees, 1,056 bytes allocated.
==14811== For counts of detected errors, rerun with: -v
==14811== All heap blocks were freed -- no leaks are possible.
OPEN OPERATION: Permission denied
==14808== Syscall param read(buf) contains uninitialised byte(s)
==14808== at 0x40007F2: (within /lib/ld-2.7.so)
==14808== by 0x405F44F: (below main) (in /lib/i686/cmov/libc-2.7.so)
==14808== Warning: invalid file descriptor -1 in syscall read()
READ OPERATION: Bad file descriptor
==14808== Warning: invalid file descriptor -1 in syscall close()
==14808==
==14808== Conditional jump or move depends on uninitialised value(s)
==14808== at 0x4087B0E: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Use of uninitialised value of size 4
==14808== at 0x40239D8: strlen (mc_replace_strmem.c:242)
==14808== by 0x408A681: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Use of uninitialised value of size 4
==14808== at 0x40239E3: strlen (mc_replace_strmem.c:242)
==14808== by 0x408A681: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Conditional jump or move depends on uninitialised value(s)
==14808== at 0x40244B7: mempcpy (mc_replace_strmem.c:77)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Conditional jump or move depends on uninitialised value(s)
==14808== at 0x4024540: mempcpy (mc_replace_strmem.c:80)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Conditional jump or move depends on uninitialised value(s)
==14808== at 0x40244CE: mempcpy (mc_replace_strmem.c:676)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Conditional jump or move depends on uninitialised value(s)
==14808== at 0x402450E: mempcpy (mc_replace_strmem.c:676)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Conditional jump or move depends on uninitialised value(s)
==14808== at 0x4024556: mempcpy (mc_replace_strmem.c:676)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Use of uninitialised value of size 4
==14808== at 0x4024563: mempcpy (mc_replace_strmem.c:676)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== Use of uninitialised value of size 4
==14808== at 0x4024570: mempcpy (mc_replace_strmem.c:676)
==14808== by 0x40AE345: _IO_file_xsputn (in
/lib/i686/cmov/libc-2.7.so)
==14808== by 0x408A952: vfprintf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x40901D2: printf (in /lib/i686/cmov/libc-2.7.so)
==14808== by 0x8048680: main (a.c:30)
==14808==
==14808== ERROR SUMMARY: 254 errors from 11 contexts (suppressed: 11
from 1)
==14808== malloc/free: in use at exit: 0 bytes in 0 blocks.
==14808== malloc/free: 2 allocs, 2 frees, 704 bytes allocated.
==14808== For counts of detected errors, rerun with: -v
==14808== All heap blocks were freed -- no leaks are possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top