Öncelikle her processin unique dediğimiz nev-i şahsına münhasır id leri bulunur.Bunu c dilinde şu komutlarla gösterebilir.
#include <stdio.h>
Gelelim fork() komutuna.fork() komutu process kopyalamaya yarar.Kopyalanan process e child process,kopyalandığı process e parent process denir.Child process,parent process id sinden bir fazlası verilecek şekilde kopyalanır.
Burada dikkat etmemiz gereken husus return nun iki defa gerçekleşmesidir. Parent process e ve child process e aynı anda dönüş yapılır.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int forkResult;
printf("process id = %i\n",getpid());
forkResult=fork();
printf("process id = %i - result %d\n",getpid(),forkResult);
return 0;
}
Gördüğümüz gibi 2289 parent processimizn id sidir(PPID) 2290 ise child processimizin id sidir.
process id:2289 - result 2290(parent process)
process id:2289 - result 0(child process)
Write a
YanıtlaSilc
program that
does the following:
a.
Inputs the
name of the desired executable
from the user
b.
Forks a child to do the required work
c.
Waits for the child to finish
#include
YanıtlaSil#include
#include
#include
void main(){
char *arg_list[] = {"ls","-l","-a",NULL};
pid_t childPID;
childPID = fork();
if(childPID == 0){
printf("child process%i",childPID);
execvp("ls",arg_list);
}
else{
printf("main process\n");
}
printf("main processID%i",getpid());
}
like that ??
*stdio.h,stdlib.h,sys/types.h,unistd.h