十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
1.需要循環(huán)創(chuàng)建50個(gè)進(jìn)程作為某種客戶端連接服務(wù)器進(jìn)行操作,由于fork理解不夠深,如下操作
#include
#include
#include
#include

創(chuàng)新互聯(lián)建站長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為靈壽企業(yè)提供專業(yè)的網(wǎng)站制作、做網(wǎng)站,靈壽網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
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)生了無(wú)數(shù)進(jìn)程,總之沒有計(jì)算趕緊將電腦重新啟動(dòng)了。
2.fork創(chuàng)建進(jìn)程
子進(jìn)程是父進(jìn)程的復(fù)制品。例如,子進(jìn)程獲得
父進(jìn)程數(shù)據(jù)空間、堆和棧的復(fù)制品。注意,這是子進(jìn)程所擁有的拷貝。父、子進(jìn)程并不共享這
些存儲(chǔ)空間部分。如果正文段是只讀的,則父、子進(jìn)程共享正文段。
f o r k有兩種用法:
(1) 一個(gè)父進(jìn)程希望復(fù)制自己,使父、子進(jìn)程同時(shí)執(zhí)行不同的代碼段。這在網(wǎng)絡(luò)服務(wù)進(jìn)程
中是常見的——父進(jìn)程等待委托者的服務(wù)請(qǐng)求。當(dāng)這種請(qǐng)求到達(dá)時(shí),父進(jìn)程調(diào)用 f o r k,使子進(jìn)
程處理此請(qǐng)求。父進(jìn)程則繼續(xù)等待下一個(gè)服務(wù)請(qǐng)求。
(2) 一個(gè)進(jìn)程要執(zhí)行一個(gè)不同的程序。這對(duì) s h e l l是常見的情況。在這種情況下,子進(jìn)程在
從f o r k返回后立即調(diào)用e x e c
waitpid用法參考https://blog.csdn.net/u011068702/article/details/54409273
正確創(chuàng)建50個(gè)進(jìn)程
#include
#include
#include
#include
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
waitpid(g_pid[i],NULL, 0);
printf("wait the sun process\n");
}
printf("father process over\n");
return 0;
}