473,387 Members | 1,863 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.

fork

2
i need to write a (c,c++) program that has a parent and child process. The parent process forks the child, then calls a function which prints out the parent and child process id (with a tag indicating which pid is which). The parent then waits for the child to complete.
Once forked, the child calls a function which sleeps for a period of seconds where the sleep time in seconds is (command line) input as a startup parameter to the program (data sharing between parent and child). Once the child wakes up, it prints out the date (do not use system to do the date, but find the underlying OS system call) and terminates. The printout should delineate between parent and child.
Nov 1 '06 #1
2 10668
horace1
1,510 Expert 1GB
do you know how fork() works? i.e.
UNIX creates a new process with the fork system call
pid = fork();

Unix makes a clone (identical copy) of the processes
§ After the fork both processes execute in parallel
§ Original process is the parent and pid is non zero, i.e. the PID of the child
§ New process is the child and pid is zero.
§ Both processes separate, i.e. have separate copies of global and local data, etc.
§ The child and parent then execute in parallel; obviously if there is only one processor they are time-shared.
§ Open files are shared between parent and child. Initially, the child will have a copy of the parents memory, but it will be a separate copy (at least until parent or child modify it).
§ Heavyweight processes, i.e. called heavyweight because that have speperate stacks, heap, etc.

a simple program looks like
Expand|Select|Wrap|Line Numbers
  1.    #include <iostream.h>
  2.     #include <unistd.h>
  3.     int main(void)
  4.     {
  5.             int pid;
  6.             if((pid = fork()) < 0) // create new process
  7.                 cout << "Fork failed" << endl;
  8.             else
  9.                 if (pid == 0) cout << "The child" << endl;
  10.                 else          cout << "The parent, child pid " << pid << endl;
  11.             return 0;
  12.     }
  13.  
  14.  
Nov 1 '06 #2
yes..about that.. if i want n children to be created. each of them reading a command from an array .. i mean something like this
a[i] - will contain the i-th command which i want to execute
pid_t pid;
for (i=0;i<n;i++)
{
pid=fork();
if (pid==0) // the parent
wait();
else execlp( a[i],a[i],"","",NULL);
}
Is this ok? I mean.. it will generate n procceses each of them executing their command ? .. the execlp function is a bit confusing as well...
Feb 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
1
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as...
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
9
by: Gilles Ganault | last post by:
Hello I need to launch a Python script, and fork it so that the calling script can resume with the next step will the Python script keeps running. I tried those two, but they don't work, as...
2
by: Radz | last post by:
when the fork system call is executed, a new process is created. The original process is called the parent process whereas the new process is called the child process. The new process consists of a...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.