文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>进程和程序

进程和程序

时间:2010-10-21  来源:linyilong

 

 

代码
 1 #include <unistd.h>
2 #include <string.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #define MAXARGS 20
8 #define ARGLEN 100
9
10 int main(void)
11 {
12 char *arglist[MAXARGS+1];
13 int numargs;
14 char argbuf[ARGLEN];
15 char *makestring();
16
17 numargs=0;
18
19 while(numargs < MAXARGS)
20 {
21 printf("Arg[%d]:",numargs);
22 if(fgets(argbuf,ARGLEN,stdin) && *argbuf != '\n')
23 arglist[numargs++]=makestring(argbuf);
24 else
25 {
26 if(numargs>0)
27 {
28 arglist[numargs]=NULL;
29 execute(arglist);
30 numargs=0;
31 }
32 }
33 }
34 return 0;
35 }
36
37 int execute(char *arglist[])
38 {
39 execvp(arglist[0],arglist);
40 perror("execvp failed");
41 exit(1);
42 }
43
44 char *makestring(char *buf)
45 {
46 char *cp;
47 buf[strlen(buf)-1]='\0';
48 cp=(char *)malloc(strlen(buf)+1);
49 if(cp==NULL)
50 {
51 fprintf(stderr,"no memory\n");
52 exit(1);
53 }
54 strcpy(cp,buf);
55 return cp;
56 }
57

 

 pid_t result=fork(void);

 

 

代码
#include <unistd.h>
#include
<stdio.h>

int main(void)
{
int ret_from_fork,mypid;

mypid
=getpid();
printf(
"Before:my pid is %d\n",mypid);

ret_from_fork
=fork();

sleep(
1);

printf(
"After my pid id %d,fork() said %d\n",getpid(),ret_from_fork);
}

 

 

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

int main(void)
{
printf(
"my pid is %d\n",getpid());
fork();
fork();
fork();
printf(
"my pid is %d\n",getpid());
}

 

 

代码
#include <unistd.h>
#include
<stdio.h>

int main(void)
{
int fork_rv;
printf(
"before %d\n",getpid());
fork_rv
=fork();

if(fork_rv==-1)
perror(
"fork");
else if(fork_rv==0)
printf(
"child:%d\n",getpid());
else
printf(
"parent:%d,my child is %d\n",getpid(),fork_rv);
}

 

#include <sys/types.h>

#include <sys/wait.h>

pid_t result=wait(int *status);

 

 

代码
#include <unistd.h>
#include
<stdio.h>
#include
<stdlib.h>

#define DELAY 2

int main(void)
{
int newpid;
void child_code(),parent_code();

printf(
"before:my pid is %d\n",getpid());
if((newpid=fork())==-1)
perror(
"fork");
else if(newpid==0)
child_code(DELAY);
else
parent_code(newpid);
}

void child_code(int delay)
{
printf(
"child %d here,will sleep for %d seconds\n",getpid(),delay);
sleep(delay);
printf(
"child done about to exit\n");
exit(
17);
}

void parent_code(int childpid)
{
int wait_rv;
wait_rv
=wait(NULL);
printf(
"done waiting for %d,wait returned %d\n",childpid,wait_rv);
}

 

 

代码
#include <stdlib.h>
#include
<unistd.h>
#include
<stdio.h>

#define DELAY 5

void child_code();
void parent_code();
int main(void)
{
pid_t newpid;
printf(
"before:my pid is %d\n",getpid());

if((newpid=fork())==-1)
perror(
"fork");
else if(newpid==0)
child_code(DELAY);
else
parent_code();
}

void child_code(int delay)
{
printf(
"child %d here will sleep for %d seconds\n",getpid(),delay);
sleep(delay);
printf(
"child done\n");
exit(
17);
}

void parent_code(int childpid)
{
int wait_rv;
int child_status;
int high_8,low_7,bit_7;

wait_rv
=wait(&child_status);
printf(
"done waiting for %d,wait return %d\n",childpid,wait_rv);

high_8
=child_status>>8;
low_7
=child_status & 0x7F;
bit_7
=child_status & 0x80;
printf(
"status:exit=%d,sig=%d,core=%d\n",high_8,low_7,bit_7);
}

 

shell:

代码
#include <unistd.h>
#include
<stdlib.h>
#include
<string.h>
#include
<stdio.h>
#include
<signal.h>

#define MAXARGS 20
#define ARGLEN 100

char *makestring(char *argbuf);
void execute(char *arglist[]);

int main(void)
{
char *arglist[MAXARGS+1];
int numargs=0;
char argbuf[ARGLEN];
while(numargs < MAXARGS)
{
printf(
"Arg[%d]:",numargs);
if(fgets(argbuf,ARGLEN,stdin) && *argbuf != '\n')
arglist[numargs
++]=makestring(argbuf);
else
{
if(numargs > 0)
{
arglist[numargs]
=NULL;
execute(arglist);
numargs
=0;
}
}
}
return 0;
}


char *makestring(char *argbuf)
{
char *cp;
argbuf[strlen(argbuf)
-1]='\0';
cp
=(char *)malloc(strlen(argbuf)+1);
if(cp==NULL)
{
fprintf(stderr,
"no memory\n");
exit(
1);
}
strcpy(cp,argbuf);
return cp;
}

void execute(char *arglist[])
{
int pid,exitstatus;
pid
=fork();

switch(pid)
{
case -1:
perror(
"fork error");
exit(
1);
case 0:
execvp(arglist[
0],arglist);
perror(
"execvp failed");
exit(
1);
default:
while(wait(&exitstatus) != pid)
;
printf(
"child exited with status %d,%d\n",exitstatus>>8,exitstatus &0377);
}
}

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载