473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

/*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
4 1726
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Maybe I ask something OS depended but maybe not. So I ask: I want to run an external program from my C++ program. I want stdin of external program constructed from a C++ string in my program....
6
by: Rob Bolton | last post by:
Hi there, If a program running under the interactive logon session (say Susan), needs to impersonate Bob (via "LogonUser()"), but Bob needs to access the network as Susan (i.e., his local...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
7
by: newgoat | last post by:
I just wrote a short program to see process switch when sleep() is invoked within a process. The code is as follows: #include<stdio.h> #include<unistd.h> #include<sys/types.h>...
10
by: preethamkumark | last post by:
- The program first creates a shared memory buffer containing an array of 20 integers. - Each slot of the buffer can have either 0 or 1, where 0 represents an empty slot, and 1 represents an...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
7
by: Sheikko | last post by:
Hi all, i have an application and i wan to call another program into it, like notepad, so when i minimize the notepad, or maximize, it must rest in my application. thank you
0
by: eso40043 | last post by:
Hello, due to a memory leak (bug, I guess) in pyraf(or rather in IRAF) I have to fork an iterative process that goes through hundreds of image frames and does unspeakable things to them. In the...
4
by: jewel87 | last post by:
Hi everyone! I'm writing some code in C under UNIX, which should give some output like this: PARENT: pid = 10063 CHILD: my pid = 10064 CHILD: my parent's pid = 10063 CHILD: Sleeping... PARENT:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.