By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,736 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

a simple program to illustrate that child process does not shareresources with parent process

P: n/a
/*here is the program */
#include<stdio.h>
#include<sched.h>
#include<sys/types.h>
#include<fcntl.h>
int main(){
pid_t chpid;
int fd,variable,status;
char ch;
variable=9;
if(fd=open("hello.c",O_RDONLY)==-1) {
printf("unable to open hello.c\n");
return 1;
}

chpid=fork();
if(chpid!=0) {
wait(&status);
}

else {
variable=42;
close(fd);
printf("the child has changed the value of variable to %d
\n",variable);
printf("the child has also closed the file\n");
return 0;
}

printf("the value of variable now is %d\n",variable);

if(read(fd,&ch,1)==-1) {
printf("READ Failed\n");
return 1;
}
else {
printf("Read from file %s",&ch);
}
return 0;
}
/*

*/
the output is as follows

the child has changed the value of variable to 42
the child has also closed the file
the value of variable now is 9

The first three lines are as expected. but after the program goes into
the read function second time it does not come out again Why?
Nov 7 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sr************@gmail.com wrote:
/*here is the program */
#include<stdio.h>
#include<sched.h>
#include<sys/types.h>
#include<fcntl.h>
(fx:snip)

(a) comp.unix.programmer

(b) FIX YOUR LAYOUT.

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Nov 7 '08 #2

P: n/a
In article <e2**********************************@r37g2000prr. googlegroups.com>,
<sr************@gmail.comwrote:
char ch;
....
if(read(fd,&ch,1)==-1) {
....
printf("Read from file %s",&ch);
The argument for %s needs to be a null-terminated string.
if(fd=open("hello.c",O_RDONLY)==-1) {
== binds more tightly than =. Turning up the warning level on your
compiler would probably have caught this.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 7 '08 #3

P: n/a
On Fri, 7 Nov 2008 05:03:10 -0800 (PST),
sr************@gmail.com <sr************@gmail.comwrote:

While almost all of your program is Unix specific, rather than standard
C, your error is a language error:
if(fd=open("hello.c",O_RDONLY)==-1) {
This is the same as

if (fd = (open("hello.c",O_RDONLY) == -1)) {

which is not what you meant. You meant

if ((fd = open("hello.c",O_RDONLY)) == -1) {

Most likely you can get your compiler to warn you about this. If you use
gcc, make sure to include -W and -Wall as command line options.

You also forgot to include some headers that your compiler can warn you
about. If you need mroe help with this program, I suggest you post to
comp.unix.programmer in the future. There are more people there who can
help you with the Unix specific bits.

Martien
--
|
Martien Verbruggen | The Second Law of Thermodenial: In any
| closed mind the quantity of ignorance
| remains constant or increases.
Nov 7 '08 #4

P: n/a
thanks a lot... i was looking at the second read call in the
program... forgot all about precedences of operators.

Martien Verbruggen wrote:
On Fri, 7 Nov 2008 05:03:10 -0800 (PST),
sr************@gmail.com <sr************@gmail.comwrote:

While almost all of your program is Unix specific, rather than standard
C, your error is a language error:
if(fd=open("hello.c",O_RDONLY)==-1) {

This is the same as

if (fd = (open("hello.c",O_RDONLY) == -1)) {

which is not what you meant. You meant

if ((fd = open("hello.c",O_RDONLY)) == -1) {

Most likely you can get your compiler to warn you about this. If you use
gcc, make sure to include -W and -Wall as command line options.

You also forgot to include some headers that your compiler can warn you
about. If you need mroe help with this program, I suggest you post to
comp.unix.programmer in the future. There are more people there who can
help you with the Unix specific bits.

Martien
--
|
Martien Verbruggen | The Second Law of Thermodenial: In any
| closed mind the quantity of ignorance
| remains constant or increases.
Nov 9 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.