溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux fork的頭次使用

發(fā)布時間:2020-07-12 01:18:07 來源:網(wǎng)絡(luò) 閱讀:214 作者:寒苦梅花 欄目:編程語言

1.需要循環(huán)創(chuàng)建50個進程作為某種客戶端連接服務(wù)器進行操作,由于fork理解不夠深,如下操作
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <stdio.h>

int num1 = 0;
int num2 = 0;
int add(int pid)
{
int i = 0;
for(i=0; i<10; i++)
{
printf("My Process id is=%d, sum=%d\n", pid, num1+num2);
num1++;
num2++;
sleep(2);
}
printf("the process over id=%d\n", pid);
return 0;
}

int main()
{
pid_t fpid;
int count =0;
int i = 0;
printf("input the fork count:\n");
scanf("%d", &count);
for(i=0; i< count; i++)
{

  if(fpid< 0)
  {
     printf("fork error\n");
     exit(-1);
  }else if(fpid == 0)
  {
     printf("I am the child process, my Process id is=%d, fpid=%d\n", getpid(), fpid);
     add(getpid());
  }else 
  {
    printf("I am the parent process, my process id is=%d, fpid=%d\n", getpid(),fpid);
  }

}

return 0;
}
輸入50后,產(chǎn)生了無數(shù)進程,總之沒有計算趕緊將電腦重新啟動了。

2.fork創(chuàng)建進程

子進程是父進程的復(fù)制品。例如,子進程獲得
父進程數(shù)據(jù)空間、堆和棧的復(fù)制品。注意,這是子進程所擁有的拷貝。父、子進程并不共享這
些存儲空間部分。如果正文段是只讀的,則父、子進程共享正文段。
f o r k有兩種用法:
(1) 一個父進程希望復(fù)制自己,使父、子進程同時執(zhí)行不同的代碼段。這在網(wǎng)絡(luò)服務(wù)進程
中是常見的——父進程等待委托者的服務(wù)請求。當(dāng)這種請求到達時,父進程調(diào)用 f o r k,使子進
程處理此請求。父進程則繼續(xù)等待下一個服務(wù)請求。
(2) 一個進程要執(zhí)行一個不同的程序。這對 s h e l l是常見的情況。在這種情況下,子進程在
從f o r k返回后立即調(diào)用e x e c

waitpid用法參考https://blog.csdn.net/u011068702/article/details/54409273

正確創(chuàng)建50個進程

#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <stdio.h>

int num1 = 0;
int num2 = 0;
int g_pid[50] = {0};
int g_num = 0;
int add(int pid)
{
int i = 0;
for(i=0; i<10; i++)
{
printf("My Process id is=%d, sum=%d\n", pid, num1+num2);
num1++;
num2++;
sleep(2);
}
printf("the process over id=%d\n", pid);
return 0;
}

int worker(int i)
{
int pid = fork();
switch(pid)
{
case 0:
g_pid[i] = pid;
printf("I am the child process, my Process id is=%d, fpid=%d\n", getpid(), pid);
add(getpid());
exit(0);;
case -1:
printf("[Worker]: Fork failed!\n");
exit(0);
default:
break;
}
}

int main()
{
pid_t fpid;
int count =0;
int i = 0;
printf("input the fork count:\n");
scanf("%d", &count);
g_num = count;
for(i=0; i< count; i++)
{

worker(i);

/ fpid = fork();
if(fpid< 0)
{
printf("fork error\n");
exit(-1);
}else if(fpid > 0)
{
printf(" I am the parent process, my process id is=%d, fpid=%d\n", getpid(),fpid);
continue;
}else
{
printf("I am the child process, my Process id is=%d, fpid=%d\n", getpid(), fpid);
add(getpid());
}
/
/ if(fpid< 0)
{
printf("fork error\n");
exit(-1);
}else if(fpid == 0)
{
printf("I am the child process, my Process id is=%d, fpid=%d\n", getpid(), fpid);
add(getpid());
}else
{
printf("I am the parent process, my process id is=%d, fpid=%d\n", getpid(),fpid);
}
/
}

for(i=0; i<g_num; i++)
{
waitpid(g_pid[i],NULL, 0);
printf("wait the sun process\n");
}
printf("father process over\n");
return 0;
}

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI