註冊 登入
鐵之狂傲 返回首頁

亡眼的家園 https://www.gamez.com.tw/?135404 [收藏] [複製] [分享] [RSS]

網誌

code

熱度 5已有 117 次閱讀12-12-4 16:31

#include<stdio.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<sys/shm.h>
#include<signal.h>
#include<unistd.h>

void edt(int sig)
{
printf("child process finish!! \n");
}
int shmid=0;

int main(void)
{
//pid_t childpid;
//childpid=fork();
shmid=shmget(IPC_PRIVATE,sizeof(int),S_IRUSR|S_IWUSR);
int *spt1=(int*)shmat(shmid,NULL,0);
int *spt2=(int*)shmat(shmid,NULL,0);
pid_t childpid;
childpid=fork();

if(childpid==0)
{
sleep(1);
int a=51,b=0;
for(a;a<101;a++){b=a+b;}
        if(a==101)
        {
        *spt1=*spt1+b;
        printf("a=%d ,b= %d \n",a,*spt1);
        kill(getppid(),SIGALRM);
        }
}
if(childpid>0)
{
int a=1,b=0;
for(a;a<51;a++){b=a+b;}
printf("parent is %ld ,child is %ld \n",(long)getpid(),(long)childpid);
(void)signal(SIGALRM,edt);
wait(NULL);
*spt2=*spt1+b;
printf("b= %d \n",*spt2);
return 0;
}
}


                                  
5

路過

雷人

握手

鮮花

雞蛋

剛表態過的朋友 (5 人)

評論 (0 個評論)

facelist doodle 塗鴉板

你需要登入後才可以評論 登入 | 註冊

存檔|手機版|聯絡我們|新聞提供|鐵之狂傲

GMT+8, 24-5-3 10:16 , Processed in 0.007827 second(s), 10 queries , Gzip On.

回頂部