- 论坛徽章:
- 0
|
#include <unistd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 50
int main(void)
{
char buf[MAXLINE];
pid_t pid;
int status,i;
// char lsargv[] ={“ls”,”-l”, 0};
// char lsenvp[] ={“PATH=/bin:/usr/bin”, 0};
printf("Now it is before fork \n");
if (fgets(buf, MAXLINE, stdin) != NULL)
{
buf[strlen(buf)] ='\0'; /* replace newline with null */
printf("the buf is %s\n",buf);
if ( (pid = fork()) < 0)
perror("fork");
else if (pid == 0)
{ /* child */
/* execlp(buf, buf,"-l", (char *) 0);*/
i=execlp(buf,buf, (char*) 0);
if (i==-1)
perror("execlp");
exit(1);
}
/* parent */
else if ( (pid = waitpid(pid, &status, 0)) < 0)
perror("waitpid");
printf("the status is %d ",status);
printf("the size of short is %d\n",sizeof(unsigned));
}
exit(0);
}
输入/bin/ls后
输出为
Now it is before fork
/bin/ls
the buf is /bin/ls
execlp: No such file or directory
the status is 256 the size of short is 4 |
|