SlideShare a Scribd company logo
http://linux.die.net/man/3/exec
fork() creates a new process by duplicating the calling process.
The new process, referred to as the child, is an exact duplicate
of the calling process, referred to as the parent#include
<unistd.h>pid_t fork(void);
The exec() family of functions replaces the current process
image with a new process image. The functions described in this
manual page are front-ends for execve(2). (See the manual page
for execve(2) for further details about the replacement of the
current process image.)
The exec() family of functions include execl, execlp, execle,
execv, execvp, and execvpe to execute a file.
The ANSI prototype for execl() is:
int execl(const char *path, const char *arg0,..., const char
*argn, 0)
http://www.cems.uwe.ac.uk/~irjohnso/coursenotes/lrc/system/pc
/pc4.htm #inciude <stdio.h> #inciude <unistd.h> main()
{ execl("/bin/ls", "ls", "-l", 0); printf("Can
only get here on errorn"); }
The first parameter to execl() in this example is the full
pathname to the ls command. This is the file whose contents
will be run, provided the process has execute permission on the
file. The rest of the execl() parameters provide the strings to
which the argv array elements in the new program will point. In
this example, it means that the ls program will see the
string ls pointed to by its argv[0], and the string -l pointed to by
itsargv[1]. In addition to making all these parameters available
to the new program, the exec() calls also pass a value for the
variable: extern char **environ;
This variable has the same format as the argv variable except
that the items passed via environ are the values in the
environment of the process (like any exported shell variables),
rather than the command line parameters. In the case of execl(),
the value of the environ variable in the new program will be a
copy of the value of this variable in the calling process.
The execl() version of exec() is fine in the circumstances where
you can ex-plicitly list all of the parameters, as in the previous
example. Now suppose you want to write a program that doesn't
just run ls, but will run any program you wish, and pass it any
number of appropriate command line parameters.
Obviously, execl() won't do the job.
The example program below, which implements this
requirement, shows, however, that the system call execv() will
perform as required: #inciude <stdio.h> main(int argc, char
**argv) { if (argc==1) {
printf("Usage: run <command> [<paraneters>]n");
exit(1) } execv(argv[l], &argv[1));
printf("Sorry... couldn't run that!n"); }
The prototype for execv() shows that it only takes two
parameters, the first is the full pathname to the command to
execute and the second is the argv value you want to pass into
the new program. In the previous example this value was
derived from the argv value passed into the run command, so
that the run command can take the command line parameter
values you pass it and just pass them on. int execl(pathname,
argo, ..., argn, 0); int execv(pathname, argv); int
execlp(cmdname, arg0, ..., argn, 0); int execvp(cmdname,
argv); int execle(patbname, arg0, ..., arga, 0, envp); int
execve(pathname, argv, envp); char *pathname, *cmdname;
char *arg0, ..., *argn; char **argv, **envp;
pipe() creates a pipe, a unidirectional data channel that can be
used for interprocess communication. The array pipefd is used
to return two file descriptors referring to the ends of the
pipe. pipefd[0] refers to the read end of the
pipe. pipefd[1] refers to the write end of the pipe. Data written
to the write end of the pipe is buffered by the kernel until it is
read from the read end of the pipe#include <unistd.h>int
pipe(int pipefd[2]);#define _GNU_SOURCE /* See
feature_test_macros(7) */#include<fcntl.h> /* Obtain
O_* constant definitions */#include <unistd.h>int pipe2(int
pipefd[2], int flags);
read() attempts to read up to count bytes from file
descriptor fd into the buffer starting at buf.#include
<unistd.h>ssize_t read(int fd, void *buf, size_t count);
write() writes up to count bytes from the buffer pointed buf to
the file referred to by the file descriptor fd.#include
<unistd.h>ssize_t write(int fd, const void *buf, size_t count);
close() closes a file descriptor, so that it no longer refers to any
file and may be reused. Any record locks (see fcntl(2)) held on
the file it was associated with, and owned by the process, are
removed (regardless of the file descriptor that was used to
obtain the lock).
If fd is the last file descriptor referring to the underlying open
file description (seeopen(2)), the resources associated with the
open file description are freed; if the descriptor was the last
reference to a file which has been removed using unlink(2) the
file is deleted.#include <unistd.h>int close(int fd);
Sample Program 1
/****************************************************
************ * * Example: to demonstrate fork() and execl()
and system calls *
*****************************************************
**********/#include <stdio.h>#include <stdlib.h>#include
<sys/types.h>#include <unistd.h>#include <sys/wait.h>int
main( int argc, char *argv[], char *env[] ){ pid_t my_pid,
parent_pid, child_pid; int status;/* get and print my pid and
my parent's pid. */ my_pid = getpid(); parent_pid =
getppid(); printf("n Parent: my pid is %dnn", my_pid);
printf("Parent: my parent's pid is %dnn", parent_pid);/* print
error message if fork() fails */ if((child_pid = fork()) < 0 ) {
perror("fork failure"); exit(1); }/* fork() == 0 for child
process */ if(child_pid == 0) { printf("nChild: I am a new-
born process!nn"); my_pid = getpid(); parent_pid =
getppid(); printf("Child: my pid is: %dnn", my_pid);
printf("Child: my parent's pid is: %dnn", parent_pid);
printf("Child: I will sleep 3 seconds and then execute - date -
command nn"); sleep(3); printf("Child: Now, I woke up
and am executing date command nn"); execl("/bin/date",
"date", 0, 0); perror("execl() failure!nn"); printf("This
print is after execl() and should not have been executed if execl
were successful! nn"); _exit(1); }/* * parent process */
else { printf("nParent: I created a child process.nn");
printf("Parent: my child's pid is: %dnn", child_pid);
system("ps -acefl | grep ercal"); printf("n n");
wait(&status); /* can use wait(NULL) since exit status
from child is not used. */ printf("n Parent: my child is dead.
I am going to leave.n n "); } return 0;}
Sample Program 2
Execution > gcc –o proc proc.c> gcc –o fibo fibo.c> gcc –o pipe
pipe.c> ./proc
proc.c#include <stdio.h>#include <sys/shm.h>#include
<sys/stat.h>#include <sys/types.h>#include <unistd.h>int
main(){ pid_t pid; int segment_id; //allocate the memory
char *shared_memory; //pointer to memory const int size =
4096; segment_id = shmget(IPC_PRIVATE, size, S_IRUSR |
S_IWUSR); shared_memory = (char *) shmat(segment_id,
NULL, 0); pid = fork(); if(pid < 0) { //error
fprintf(stderr, "Fork failed"); return 1; } else if(pid
== 0){ //child process char *child_shared_memory;
child_shared_memory = (char *) shmat(segment_id,NULL,0);
//attach mem sprintf(child_shared_memory, "Hello
parent process!"); //write to
the shared mem shmdt(child_shared_memory);
execl("./pipe", "pipe", "3", 0); /* to call pipe program */
execl("./fibo", "fibo", "12", 0); /* to call fibo program */ }
else {//parent process wait(NULL);
printf("Child process completed.n"); printf("The
child process has written to the shared memory.n It
has said: %sn", shared_memory); //read from shared mem
shmdt(shared_memory); shmctl(segment_id,
IPC_RMID, NULL); } return 0;}
fibo.c#include <stdio.h>#include <stdlib.h>#include
<sys/types.h>#include <unistd.h>#include <string.h>int fibo(int
num);int main(int argc, char *argv[]) { if (argc != 2) {
fprintf(stderr, "argc %d n", argc); fprintf(stderr, "argv[0]:
%s >n", argv[0]); fprintf(stderr, "argv[1]: %s >n",
argv[1]); fprintf(stderr, "Usage: %s <string>n", argv[0]);
exit(EXIT_FAILURE); } int num = atoi(argv[1]); printf(
"%d'th Fibonacci number is %dn", num, fibo(num));
exit(EXIT_SUCCESS);} /* end of main */ int fibo(int num) {
if (num > 0) { if (num <=2) { return 1; } else {
return fibo(num-1) + fibo(num-2); } } else { return -1;
}} /* end of fibo() */
pipe.c#include <stdio.h>#include <stdlib.h>#include
<sys/shm.h>#include <sys/stat.h>#include <sys/wait.h>#include
<sys/types.h>#include <unistd.h>#include <string.h>int
segmentId;char *sharedMemory;int main(int argc, char *argv[])
{ int pipefd[2]; pid_t cpid; char buf; if (argc != 2) {
fprintf(stderr, "argc %d n", argc); fprintf(stderr, "argv[0]:
%s >n", argv[0]); fprintf(stderr, "argv[1]: %s >n",
argv[1]); fprintf(stderr, "Usage: %s <string>n", argv[0]);
exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) {
perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if
(cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]);
/* Close unused write end */ while (read(pipefd[0], &buf, 1)
> 0) write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "n", 1); _exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */
close(pipefd[0]); /* Close unsed read end */ write(pipefd[1],
argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will
see EOF */ wait (NULL); /* Wait for child */
exit(EXIT_SUCCESS); }}
Program Assignment : Process Management
Objective: This program assignment is given to the Operating
Systems course to allow the students to figure out how a single
process (parent process) creates a child process and how they
work on Unix/Linux(/Mac OS X/Windows) environment.
Additionally, student should combine the code for describing
inter-process communication into this assignment. Both parent
and child processes interact with each other through shared
memory-based communication scheme or message passing
scheme.
Environment: Unix/Linux environment (VM Linux or Triton
Server, or Mac OS X), Windows platform
Language: C or C++, Java
Requirements:
i. You have wide range of choices for this assignment. First,
design your program to explain the basic concept of the process
management in Unix Kernel. This main idea will be evolved to
show your understanding on inter-process communication, file
processing, etc.
ii. Refer to the following system calls:
- fork(), getpid(), family of exec(), wait(), sleep() system calls
for process management
- shmget(), shmat(), shmdt(), shmctl() for shared memory
support or
- msgget(), msgsnd(), msgrcv(), msgctl(), etc. for message
passing support
iii. The program should present that two different processes,
both parent and child, execute as they are supposed to.
iv. The output should contain the screen capture of the
execution procedure of the program.
v. Interaction between parent and child processes can be
provided through inter-process communication schemes, such as
shared-memory or message passing schemes.
vi. Result should be organized as a document which explains the
overview of your program, code, execution results, and the
conclusion including justification of your program, lessons
you've learned, comments, etc.
Note:
i. In addition, please try to understand how the local and global
variables work across the processes
ii. read() or write () functions are used to understand how they
work on the different processes.
iii. For extra credit, you can also incorporate advanced features,
like socket or thread functions, into your code.
Examples:
1. Process Creation and IPC with Shared Memory Scheme
===============================================
==============
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
pid_t pid;
int segment_id; //allocate the memory
char *shared_memory; //pointer to memory
const int size = 4096;
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR |
S_IWUSR);
shared_memory = (char *) shmat(segment_id, NULL, 0);
pid = fork();
if(pid < 0) { //error
fprintf(stderr, "Fork failed");
return 1;
}
else if(pid == 0){ //child process
char *child_shared_memory;
child_shared_memory = (char *)
shmat(segment_id,NULL,0); //attach mem
sprintf(child_shared_memory, "Hello parent
process!"); //write to the shared mem
shmdt(child_shared_memory);
}
else {//parent process
wait(NULL);
printf("Child process completed.n");
printf("The child process has written to the shared memory.nIt
has said: %sn", shared_memory); //read from shared mem
shmdt(shared_memory);
shmctl(segment_id, IPC_RMID, NULL);
}
return 0;
}
===============================================
==============
2. Process Creation and IPC with Message Passing Scheme
===============================================
==============
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#define N 20
typedef struct{
long stype;
char input[N];
}msgq;
int origin = 12345;
int main(int argv, char * argc[]){
msgq sendq;
msgq rcvq;
size_t qlen;
key_t key=1111;
pid_t pid_p, pid_c;
int msgid = msgget((key_t)1111, 0666|
IPC_CREAT); //declare the msg queue id
int shrm_id, shrm_size = 1024; //size of
memory for sharing
char * shrm_addr;
shrm_id = shmget(IPC_PRIVATE, shrm_size, S_IRUSR |
S_IWUSR); // get the id of memory for sharing
shrm_addr = (char*)shmat(shrm_id, NULL, 0);
pid_p = getpid(); // store parent_process_id into
variable ; pid_p
pid_c = fork(); // store child_process_id into
variable ; pid_c
//meaning of return value of fork()
//value < 0 : fork is failed
//value == 0 : child process
//value > 0 : parent process
if(pid_c < 0){
fprintf(stderr, "failed to fork()n"); // print error msg
by using stderr of fprintf
return 0;
}else if(pid_c ==0){
printf("start of child processn");
printf("--------------------------------nn");
printf("origin : %dn", origin);
origin++;
printf("value of origin after origin++; : %dnn",
origin);
char * c_shrm_addr = (char *)shmat(shrm_id, NULL,
0); // give shared memory to child by using shmat()
// NULL means find somewhere which has been
mapped
if(c_shrm_addr ==(void *) -1) // shmat() return (void
*)-1 when the sharing is failed
fprintf(stderr, "failed to sharen");
pid_c = getpid();
sendq.stype=1;
printf("input string that will be sent to parent process :
");
scanf("%s",sendq.input); //store the string for send to
parent into msg queue
qlen= strlen(sendq.input)+1;
if(msgsnd(msgid,&sendq, qlen ,IPC_NOWAIT)<0){
perror("msgsnd");
exit(0);
}
else{
printf("Msgsend : %sn", sendq.input);
}
printf("n--------------------------------n");
printf("end of child process n");
} else {
//now in parent process
printf("nstart of parent processn");
printf("--------------------------------nn");
char str[N];
int i = 0;
wait(NULL); //wait until the child process
finished
printf("n--------------------------------n");
printf("back to the parent process n");
printf("--------------------------------nn");
printf("origin : %dn", origin);
if(origin == 12345)
printf("changed origin value in child is not
applied to parentnn");
if(origin == 12346)
printf("changed origin value in child is applied ro
parentnn");
if((msgid = msgget(key,0666))<0){ //in case of
error in msgget
perror("msgget");
exit(1);
}
if(msgrcv(msgid,&rcvq,N+1,1,0)==0){ //in case of
error in msgrcv
perror("msgrcv");
exit(1);
}
printf("recieve : %snn",rcvq.input);
printf("child process refers to the memory of parent :
%xn", (int)shrm_addr);
pid_p = getpid(IPC_PRIVATE, IPC_CREAT);
printf("ID of Parent : %dn", pid_p);
printf("ID of Child : %dn", pid_c);
printf("n--------------------------------n");
printf("end of parent processnn");
}
shmdt(shrm_addr);
shmctl(shrm_id, IPC_RMID, NULL);
return 0;
}
===============================================
============== 3. Compiling and executing C codes
$ cc msgpass.c -o msgpass
$ ./msgpass
httplinux.die.netman3execfork() creates a new process by.docx

More Related Content

Similar to httplinux.die.netman3execfork() creates a new process by.docx

Help Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfHelp Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdf
mohdjakirfb
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
ChereCheek752
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
Matt Provost
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
DIPESH30
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
Thierry Gayet
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
Atul Setu
 
Cell processor lab
Cell processor labCell processor lab
Cell processor lab
coolmirza143
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
Shakila Mahjabin
 
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
AdrianEBJKingr
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
Tigabu Yaya
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
arshiartpalace
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)
Giovanni Bechis
 

Similar to httplinux.die.netman3execfork() creates a new process by.docx (20)

Help Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfHelp Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdf
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
Cell processor lab
Cell processor labCell processor lab
Cell processor lab
 
Npc14
Npc14Npc14
Npc14
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
 
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
-- This is the shell-c Test- --shell -test sub #include -ctype-h- -- C.pdf
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
Unit 4
Unit 4Unit 4
Unit 4
 
lec4.docx
lec4.docxlec4.docx
lec4.docx
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)
 

More from adampcarr67227

You are a project manager and believe that your initiative would be .docx
You are a project manager and believe that your initiative would be .docxYou are a project manager and believe that your initiative would be .docx
You are a project manager and believe that your initiative would be .docx
adampcarr67227
 
You are a project manager at a food agricultural organization and yo.docx
You are a project manager at a food agricultural organization and yo.docxYou are a project manager at a food agricultural organization and yo.docx
You are a project manager at a food agricultural organization and yo.docx
adampcarr67227
 
You are a nursing educator and you are given an assignment to teach .docx
You are a nursing educator and you are given an assignment to teach .docxYou are a nursing educator and you are given an assignment to teach .docx
You are a nursing educator and you are given an assignment to teach .docx
adampcarr67227
 
You are a paralegal working at law office of James Adams, Esq. On No.docx
You are a paralegal working at law office of James Adams, Esq. On No.docxYou are a paralegal working at law office of James Adams, Esq. On No.docx
You are a paralegal working at law office of James Adams, Esq. On No.docx
adampcarr67227
 
you are a paralegal working at the law office of Smith & Smith. The .docx
you are a paralegal working at the law office of Smith & Smith. The .docxyou are a paralegal working at the law office of Smith & Smith. The .docx
you are a paralegal working at the law office of Smith & Smith. The .docx
adampcarr67227
 
You are a police officer who has been selected to participate in a p.docx
You are a police officer who has been selected to participate in a p.docxYou are a police officer who has been selected to participate in a p.docx
You are a police officer who has been selected to participate in a p.docx
adampcarr67227
 
You are a newly-minted, tax-paying and law-abiding, permanent res.docx
You are a newly-minted, tax-paying and law-abiding, permanent res.docxYou are a newly-minted, tax-paying and law-abiding, permanent res.docx
You are a newly-minted, tax-paying and law-abiding, permanent res.docx
adampcarr67227
 
You are a new university police chief in a medium-sized city, an.docx
You are a new university police chief in a medium-sized city, an.docxYou are a new university police chief in a medium-sized city, an.docx
You are a new university police chief in a medium-sized city, an.docx
adampcarr67227
 
You are a native speaker of French living in a mainly English speaki.docx
You are a native speaker of French living in a mainly English speaki.docxYou are a native speaker of French living in a mainly English speaki.docx
You are a native speaker of French living in a mainly English speaki.docx
adampcarr67227
 
You are a new high school teacher, and have been captured at the end.docx
You are a new high school teacher, and have been captured at the end.docxYou are a new high school teacher, and have been captured at the end.docx
You are a new high school teacher, and have been captured at the end.docx
adampcarr67227
 
You are a member of the Human Resource Department of a medium-sized .docx
You are a member of the Human Resource Department of a medium-sized .docxYou are a member of the Human Resource Department of a medium-sized .docx
You are a member of the Human Resource Department of a medium-sized .docx
adampcarr67227
 
You are a network analyst on the fly-away team for the FBIs cyberse.docx
You are a network analyst on the fly-away team for the FBIs cyberse.docxYou are a network analyst on the fly-away team for the FBIs cyberse.docx
You are a network analyst on the fly-away team for the FBIs cyberse.docx
adampcarr67227
 
You are a member of the senior management staff at XYZ Corporation. .docx
You are a member of the senior management staff at XYZ Corporation. .docxYou are a member of the senior management staff at XYZ Corporation. .docx
You are a member of the senior management staff at XYZ Corporation. .docx
adampcarr67227
 
You are a member of the senior hospital administration. You become a.docx
You are a member of the senior hospital administration. You become a.docxYou are a member of the senior hospital administration. You become a.docx
You are a member of the senior hospital administration. You become a.docx
adampcarr67227
 
YOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docx
YOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docxYOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docx
YOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docx
adampcarr67227
 
You are a member of the Human Resource Department of a medium-si.docx
You are a member of the Human Resource Department of a medium-si.docxYou are a member of the Human Resource Department of a medium-si.docx
You are a member of the Human Resource Department of a medium-si.docx
adampcarr67227
 
You are a member of the American Indian tribe. Think about how your .docx
You are a member of the American Indian tribe. Think about how your .docxYou are a member of the American Indian tribe. Think about how your .docx
You are a member of the American Indian tribe. Think about how your .docx
adampcarr67227
 
You are a juvenile justice consultant creating a proposal that w.docx
You are a juvenile justice consultant creating a proposal that w.docxYou are a juvenile justice consultant creating a proposal that w.docx
You are a juvenile justice consultant creating a proposal that w.docx
adampcarr67227
 
You are a journalist and you have been sent off to write a story abo.docx
You are a journalist and you have been sent off to write a story abo.docxYou are a journalist and you have been sent off to write a story abo.docx
You are a journalist and you have been sent off to write a story abo.docx
adampcarr67227
 
You are a juvenile court probation officer. You have a choice of.docx
You are a juvenile court probation officer. You have a choice of.docxYou are a juvenile court probation officer. You have a choice of.docx
You are a juvenile court probation officer. You have a choice of.docx
adampcarr67227
 

More from adampcarr67227 (20)

You are a project manager and believe that your initiative would be .docx
You are a project manager and believe that your initiative would be .docxYou are a project manager and believe that your initiative would be .docx
You are a project manager and believe that your initiative would be .docx
 
You are a project manager at a food agricultural organization and yo.docx
You are a project manager at a food agricultural organization and yo.docxYou are a project manager at a food agricultural organization and yo.docx
You are a project manager at a food agricultural organization and yo.docx
 
You are a nursing educator and you are given an assignment to teach .docx
You are a nursing educator and you are given an assignment to teach .docxYou are a nursing educator and you are given an assignment to teach .docx
You are a nursing educator and you are given an assignment to teach .docx
 
You are a paralegal working at law office of James Adams, Esq. On No.docx
You are a paralegal working at law office of James Adams, Esq. On No.docxYou are a paralegal working at law office of James Adams, Esq. On No.docx
You are a paralegal working at law office of James Adams, Esq. On No.docx
 
you are a paralegal working at the law office of Smith & Smith. The .docx
you are a paralegal working at the law office of Smith & Smith. The .docxyou are a paralegal working at the law office of Smith & Smith. The .docx
you are a paralegal working at the law office of Smith & Smith. The .docx
 
You are a police officer who has been selected to participate in a p.docx
You are a police officer who has been selected to participate in a p.docxYou are a police officer who has been selected to participate in a p.docx
You are a police officer who has been selected to participate in a p.docx
 
You are a newly-minted, tax-paying and law-abiding, permanent res.docx
You are a newly-minted, tax-paying and law-abiding, permanent res.docxYou are a newly-minted, tax-paying and law-abiding, permanent res.docx
You are a newly-minted, tax-paying and law-abiding, permanent res.docx
 
You are a new university police chief in a medium-sized city, an.docx
You are a new university police chief in a medium-sized city, an.docxYou are a new university police chief in a medium-sized city, an.docx
You are a new university police chief in a medium-sized city, an.docx
 
You are a native speaker of French living in a mainly English speaki.docx
You are a native speaker of French living in a mainly English speaki.docxYou are a native speaker of French living in a mainly English speaki.docx
You are a native speaker of French living in a mainly English speaki.docx
 
You are a new high school teacher, and have been captured at the end.docx
You are a new high school teacher, and have been captured at the end.docxYou are a new high school teacher, and have been captured at the end.docx
You are a new high school teacher, and have been captured at the end.docx
 
You are a member of the Human Resource Department of a medium-sized .docx
You are a member of the Human Resource Department of a medium-sized .docxYou are a member of the Human Resource Department of a medium-sized .docx
You are a member of the Human Resource Department of a medium-sized .docx
 
You are a network analyst on the fly-away team for the FBIs cyberse.docx
You are a network analyst on the fly-away team for the FBIs cyberse.docxYou are a network analyst on the fly-away team for the FBIs cyberse.docx
You are a network analyst on the fly-away team for the FBIs cyberse.docx
 
You are a member of the senior management staff at XYZ Corporation. .docx
You are a member of the senior management staff at XYZ Corporation. .docxYou are a member of the senior management staff at XYZ Corporation. .docx
You are a member of the senior management staff at XYZ Corporation. .docx
 
You are a member of the senior hospital administration. You become a.docx
You are a member of the senior hospital administration. You become a.docxYou are a member of the senior hospital administration. You become a.docx
You are a member of the senior hospital administration. You become a.docx
 
YOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docx
YOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docxYOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docx
YOU ARE A MEMBER OF THE SENIOR HOSPITAL ADMINISTRATI.docx
 
You are a member of the Human Resource Department of a medium-si.docx
You are a member of the Human Resource Department of a medium-si.docxYou are a member of the Human Resource Department of a medium-si.docx
You are a member of the Human Resource Department of a medium-si.docx
 
You are a member of the American Indian tribe. Think about how your .docx
You are a member of the American Indian tribe. Think about how your .docxYou are a member of the American Indian tribe. Think about how your .docx
You are a member of the American Indian tribe. Think about how your .docx
 
You are a juvenile justice consultant creating a proposal that w.docx
You are a juvenile justice consultant creating a proposal that w.docxYou are a juvenile justice consultant creating a proposal that w.docx
You are a juvenile justice consultant creating a proposal that w.docx
 
You are a journalist and you have been sent off to write a story abo.docx
You are a journalist and you have been sent off to write a story abo.docxYou are a journalist and you have been sent off to write a story abo.docx
You are a journalist and you have been sent off to write a story abo.docx
 
You are a juvenile court probation officer. You have a choice of.docx
You are a juvenile court probation officer. You have a choice of.docxYou are a juvenile court probation officer. You have a choice of.docx
You are a juvenile court probation officer. You have a choice of.docx
 

Recently uploaded

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 

httplinux.die.netman3execfork() creates a new process by.docx

  • 1. http://linux.die.net/man/3/exec fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent#include <unistd.h>pid_t fork(void); The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.) The exec() family of functions include execl, execlp, execle, execv, execvp, and execvpe to execute a file. The ANSI prototype for execl() is: int execl(const char *path, const char *arg0,..., const char *argn, 0) http://www.cems.uwe.ac.uk/~irjohnso/coursenotes/lrc/system/pc /pc4.htm #inciude <stdio.h> #inciude <unistd.h> main() { execl("/bin/ls", "ls", "-l", 0); printf("Can only get here on errorn"); } The first parameter to execl() in this example is the full pathname to the ls command. This is the file whose contents will be run, provided the process has execute permission on the file. The rest of the execl() parameters provide the strings to which the argv array elements in the new program will point. In this example, it means that the ls program will see the string ls pointed to by its argv[0], and the string -l pointed to by itsargv[1]. In addition to making all these parameters available to the new program, the exec() calls also pass a value for the
  • 2. variable: extern char **environ; This variable has the same format as the argv variable except that the items passed via environ are the values in the environment of the process (like any exported shell variables), rather than the command line parameters. In the case of execl(), the value of the environ variable in the new program will be a copy of the value of this variable in the calling process. The execl() version of exec() is fine in the circumstances where you can ex-plicitly list all of the parameters, as in the previous example. Now suppose you want to write a program that doesn't just run ls, but will run any program you wish, and pass it any number of appropriate command line parameters. Obviously, execl() won't do the job. The example program below, which implements this requirement, shows, however, that the system call execv() will perform as required: #inciude <stdio.h> main(int argc, char **argv) { if (argc==1) { printf("Usage: run <command> [<paraneters>]n"); exit(1) } execv(argv[l], &argv[1)); printf("Sorry... couldn't run that!n"); } The prototype for execv() shows that it only takes two parameters, the first is the full pathname to the command to execute and the second is the argv value you want to pass into the new program. In the previous example this value was derived from the argv value passed into the run command, so that the run command can take the command line parameter values you pass it and just pass them on. int execl(pathname, argo, ..., argn, 0); int execv(pathname, argv); int execlp(cmdname, arg0, ..., argn, 0); int execvp(cmdname, argv); int execle(patbname, arg0, ..., arga, 0, envp); int execve(pathname, argv, envp); char *pathname, *cmdname; char *arg0, ..., *argn; char **argv, **envp;
  • 3. pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe#include <unistd.h>int pipe(int pipefd[2]);#define _GNU_SOURCE /* See feature_test_macros(7) */#include<fcntl.h> /* Obtain O_* constant definitions */#include <unistd.h>int pipe2(int pipefd[2], int flags); read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.#include <unistd.h>ssize_t read(int fd, void *buf, size_t count); write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.#include <unistd.h>ssize_t write(int fd, const void *buf, size_t count); close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock). If fd is the last file descriptor referring to the underlying open file description (seeopen(2)), the resources associated with the open file description are freed; if the descriptor was the last reference to a file which has been removed using unlink(2) the file is deleted.#include <unistd.h>int close(int fd); Sample Program 1 /**************************************************** ************ * * Example: to demonstrate fork() and execl() and system calls *
  • 4. ***************************************************** **********/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>int main( int argc, char *argv[], char *env[] ){ pid_t my_pid, parent_pid, child_pid; int status;/* get and print my pid and my parent's pid. */ my_pid = getpid(); parent_pid = getppid(); printf("n Parent: my pid is %dnn", my_pid); printf("Parent: my parent's pid is %dnn", parent_pid);/* print error message if fork() fails */ if((child_pid = fork()) < 0 ) { perror("fork failure"); exit(1); }/* fork() == 0 for child process */ if(child_pid == 0) { printf("nChild: I am a new- born process!nn"); my_pid = getpid(); parent_pid = getppid(); printf("Child: my pid is: %dnn", my_pid); printf("Child: my parent's pid is: %dnn", parent_pid); printf("Child: I will sleep 3 seconds and then execute - date - command nn"); sleep(3); printf("Child: Now, I woke up and am executing date command nn"); execl("/bin/date", "date", 0, 0); perror("execl() failure!nn"); printf("This print is after execl() and should not have been executed if execl were successful! nn"); _exit(1); }/* * parent process */ else { printf("nParent: I created a child process.nn"); printf("Parent: my child's pid is: %dnn", child_pid); system("ps -acefl | grep ercal"); printf("n n"); wait(&status); /* can use wait(NULL) since exit status from child is not used. */ printf("n Parent: my child is dead. I am going to leave.n n "); } return 0;} Sample Program 2 Execution > gcc –o proc proc.c> gcc –o fibo fibo.c> gcc –o pipe pipe.c> ./proc proc.c#include <stdio.h>#include <sys/shm.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>int
  • 5. main(){ pid_t pid; int segment_id; //allocate the memory char *shared_memory; //pointer to memory const int size = 4096; segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR); shared_memory = (char *) shmat(segment_id, NULL, 0); pid = fork(); if(pid < 0) { //error fprintf(stderr, "Fork failed"); return 1; } else if(pid == 0){ //child process char *child_shared_memory; child_shared_memory = (char *) shmat(segment_id,NULL,0); //attach mem sprintf(child_shared_memory, "Hello parent process!"); //write to the shared mem shmdt(child_shared_memory); execl("./pipe", "pipe", "3", 0); /* to call pipe program */ execl("./fibo", "fibo", "12", 0); /* to call fibo program */ } else {//parent process wait(NULL); printf("Child process completed.n"); printf("The child process has written to the shared memory.n It has said: %sn", shared_memory); //read from shared mem shmdt(shared_memory); shmctl(segment_id, IPC_RMID, NULL); } return 0;} fibo.c#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <string.h>int fibo(int num);int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "argc %d n", argc); fprintf(stderr, "argv[0]: %s >n", argv[0]); fprintf(stderr, "argv[1]: %s >n", argv[1]); fprintf(stderr, "Usage: %s <string>n", argv[0]); exit(EXIT_FAILURE); } int num = atoi(argv[1]); printf( "%d'th Fibonacci number is %dn", num, fibo(num)); exit(EXIT_SUCCESS);} /* end of main */ int fibo(int num) { if (num > 0) { if (num <=2) { return 1; } else { return fibo(num-1) + fibo(num-2); } } else { return -1; }} /* end of fibo() */ pipe.c#include <stdio.h>#include <stdlib.h>#include <sys/shm.h>#include <sys/stat.h>#include <sys/wait.h>#include
  • 6. <sys/types.h>#include <unistd.h>#include <string.h>int segmentId;char *sharedMemory;int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "argc %d n", argc); fprintf(stderr, "argv[0]: %s >n", argv[0]); fprintf(stderr, "argv[1]: %s >n", argv[1]); fprintf(stderr, "Usage: %s <string>n", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, "n", 1); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unsed read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait (NULL); /* Wait for child */ exit(EXIT_SUCCESS); }} Program Assignment : Process Management Objective: This program assignment is given to the Operating Systems course to allow the students to figure out how a single process (parent process) creates a child process and how they work on Unix/Linux(/Mac OS X/Windows) environment. Additionally, student should combine the code for describing inter-process communication into this assignment. Both parent and child processes interact with each other through shared memory-based communication scheme or message passing scheme. Environment: Unix/Linux environment (VM Linux or Triton Server, or Mac OS X), Windows platform Language: C or C++, Java Requirements: i. You have wide range of choices for this assignment. First, design your program to explain the basic concept of the process management in Unix Kernel. This main idea will be evolved to
  • 7. show your understanding on inter-process communication, file processing, etc. ii. Refer to the following system calls: - fork(), getpid(), family of exec(), wait(), sleep() system calls for process management - shmget(), shmat(), shmdt(), shmctl() for shared memory support or - msgget(), msgsnd(), msgrcv(), msgctl(), etc. for message passing support iii. The program should present that two different processes, both parent and child, execute as they are supposed to. iv. The output should contain the screen capture of the execution procedure of the program. v. Interaction between parent and child processes can be provided through inter-process communication schemes, such as shared-memory or message passing schemes. vi. Result should be organized as a document which explains the overview of your program, code, execution results, and the conclusion including justification of your program, lessons you've learned, comments, etc. Note: i. In addition, please try to understand how the local and global variables work across the processes ii. read() or write () functions are used to understand how they work on the different processes. iii. For extra credit, you can also incorporate advanced features, like socket or thread functions, into your code. Examples: 1. Process Creation and IPC with Shared Memory Scheme =============================================== ============== #include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/types.h>
  • 8. #include <unistd.h> int main(){ pid_t pid; int segment_id; //allocate the memory char *shared_memory; //pointer to memory const int size = 4096; segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR); shared_memory = (char *) shmat(segment_id, NULL, 0); pid = fork(); if(pid < 0) { //error fprintf(stderr, "Fork failed"); return 1; } else if(pid == 0){ //child process char *child_shared_memory; child_shared_memory = (char *) shmat(segment_id,NULL,0); //attach mem sprintf(child_shared_memory, "Hello parent process!"); //write to the shared mem shmdt(child_shared_memory); } else {//parent process wait(NULL); printf("Child process completed.n"); printf("The child process has written to the shared memory.nIt has said: %sn", shared_memory); //read from shared mem shmdt(shared_memory); shmctl(segment_id, IPC_RMID, NULL); } return 0; } =============================================== ============== 2. Process Creation and IPC with Message Passing Scheme
  • 9. =============================================== ============== #include <stdio.h> #include <stdlib.h> #include <sys/shm.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #define N 20 typedef struct{ long stype; char input[N]; }msgq; int origin = 12345; int main(int argv, char * argc[]){ msgq sendq; msgq rcvq; size_t qlen; key_t key=1111; pid_t pid_p, pid_c; int msgid = msgget((key_t)1111, 0666| IPC_CREAT); //declare the msg queue id int shrm_id, shrm_size = 1024; //size of memory for sharing char * shrm_addr; shrm_id = shmget(IPC_PRIVATE, shrm_size, S_IRUSR | S_IWUSR); // get the id of memory for sharing shrm_addr = (char*)shmat(shrm_id, NULL, 0); pid_p = getpid(); // store parent_process_id into variable ; pid_p pid_c = fork(); // store child_process_id into variable ; pid_c
  • 10. //meaning of return value of fork() //value < 0 : fork is failed //value == 0 : child process //value > 0 : parent process if(pid_c < 0){ fprintf(stderr, "failed to fork()n"); // print error msg by using stderr of fprintf return 0; }else if(pid_c ==0){ printf("start of child processn"); printf("--------------------------------nn"); printf("origin : %dn", origin); origin++; printf("value of origin after origin++; : %dnn", origin); char * c_shrm_addr = (char *)shmat(shrm_id, NULL, 0); // give shared memory to child by using shmat() // NULL means find somewhere which has been mapped if(c_shrm_addr ==(void *) -1) // shmat() return (void *)-1 when the sharing is failed fprintf(stderr, "failed to sharen"); pid_c = getpid(); sendq.stype=1; printf("input string that will be sent to parent process : "); scanf("%s",sendq.input); //store the string for send to parent into msg queue qlen= strlen(sendq.input)+1;
  • 11. if(msgsnd(msgid,&sendq, qlen ,IPC_NOWAIT)<0){ perror("msgsnd"); exit(0); } else{ printf("Msgsend : %sn", sendq.input); } printf("n--------------------------------n"); printf("end of child process n"); } else { //now in parent process printf("nstart of parent processn"); printf("--------------------------------nn"); char str[N]; int i = 0; wait(NULL); //wait until the child process finished printf("n--------------------------------n"); printf("back to the parent process n"); printf("--------------------------------nn"); printf("origin : %dn", origin); if(origin == 12345) printf("changed origin value in child is not applied to parentnn"); if(origin == 12346) printf("changed origin value in child is applied ro parentnn"); if((msgid = msgget(key,0666))<0){ //in case of error in msgget
  • 12. perror("msgget"); exit(1); } if(msgrcv(msgid,&rcvq,N+1,1,0)==0){ //in case of error in msgrcv perror("msgrcv"); exit(1); } printf("recieve : %snn",rcvq.input); printf("child process refers to the memory of parent : %xn", (int)shrm_addr); pid_p = getpid(IPC_PRIVATE, IPC_CREAT); printf("ID of Parent : %dn", pid_p); printf("ID of Child : %dn", pid_c); printf("n--------------------------------n"); printf("end of parent processnn"); } shmdt(shrm_addr); shmctl(shrm_id, IPC_RMID, NULL); return 0; } =============================================== ============== 3. Compiling and executing C codes $ cc msgpass.c -o msgpass $ ./msgpass