进程与控制

来源:互联网 发布:js设置cookie永不过期 编辑:程序博客网 时间:2024/05/16 00:59

关于进程控制所有的类型以及例子错的对的我都总结在一起了,希望各位大佬指点江山微笑





#include <stdio.h>

#include <sys/types.h>
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>


  
  int main1()
  {
 
 int ret =execl("/bin/ps","ps","aux",NULL);
 
 if (ret==-1)
 {
 perror("execl");
 
 }
 return 0;
  }
 
int main3()
{
int ret =execl("/mnt/hgfs/share/ldp3/file ","./file",NULL);
if (ret==-1)
 {
 perror("execl");
 
 }
 
 printf("复制完成\n");
 return 0;
}




int main2()
{
int ret=execlp("file ","./file",NULL);

if (ret==-1)
 {
 perror("execl");
 
 }
 
 printf("复制完成\n");
 return 0;



int main4()
{
printf("hello world\n");
sleep(1);
system("ls");
printf ("a\n");

}




int main5()

pid_t pid = fork();

switch (pid)
{
case -1:
perror ("fork");
break;
case 0: // 子进程
close(1);
int fd = open("child", O_RDWR|O_CREAT, 0777);
printf ("我是子进程,我的Id 是%d\n", getpid());
while (1)
{

printf ("找爸爸\n");
fflush(stdout);
sleep(2);
}
break;
default: // 父进程
printf ("我是父进程,Id = %d\n", getpid());
printf ("我走啦\n");
while (1);
exit (0);
break;
}


return 0;
}


int main6()





pid_t pid = fork();
switch (pid)
{
case -1:
perror ("fork");
break;
case 0: // 子进程
printf ("我是子进程,我的Id 是%d\n", getpid());
printf ("我走啦\n");
exit(0);
default: // 父进程
printf ("我是父进程,Id = %d\n", getpid());
while (1);
break;
}

while(1);


return 0;
}




int main8()
{
pid_t pid = fork();
switch (pid)
{
case -1:
perror ("fork");
break;
case 0: // 子进程
printf ("我是子进程,我的Id 是%d\n", getpid());
sleep(5);
exit(0);
default: // 父进程
printf ("等待子进程挂掉\n");
sleep(3);
pid_t childId = wait(NULL);

printf("父进程:  %d\n",getpid());

printf ("成功处理一个子进程, 该子进程是 : %d\n", childId);

sleep (5);

while(1);
break;
}


return 0;
}
 
 int main9()
{
int count = 10;
while (count--)
{


pid_t pid = fork();
switch (pid)
{
case -1:
perror ("fork");
break;
case 0: // 子进程
printf ("我是子进程,我的Id 是%d\n", getpid());
sleep(5);
exit(0);

default: // 父进程

printf("父进程:  %d\n",getpid());


break;
}

}

printf("等到返回的子程序进程描述符");
while (1)
{
pid_t childId = wait (NULL);

if (childId==-1)
break;
printf ("成功处理一个子进程, 该子进程是 : %d\n", childId);

}




return 0;
}


int main10()
{
pid_t pid = fork();
switch (pid)
{
case -1:
perror ("fork");
break;
case 0: // 子进程
printf ("我是子进程,我的Id 是%d\n", getpid());
while(1);
exit(0);
default: // 父进程
printf ("等待子进程挂掉\n");
printf("父进程:  %d\n",getpid());

int status;
pid_t childId = wait(&status);

printf ("成功处理一个子进程, 该子进程是 : %d, %d\n", childId, status);

if ( WIFEXITED(status))
{
printf ("正常死亡\n");
}
else
{
printf ("被人做掉\n");
}
break;
}
return 0;

 
}
int main7()
{
pid_t pid = fork();
switch (pid)
{
case -1:
perror ("fork");
break;
case 0: // 子进程
printf ("我是子进程,我的Id 是%d\n", getpid());

_exit(0);
default: // 父进程
printf ("等待子进程挂掉\n");

  
int status;
pid_t childId = waitpid(-1, NULL, WNOHANG);
printf("父进程: %d\n",getpid());
printf ("成功处理一个子进程, 该子进程是 : %d, %d\n", childId, status);
while (1);
break;
}
return 0;
}


void func()
{
printf ("333333333333333333333333\n");
// return;
 // 进程终止
 exit (0);

}


int main12()


{

printf ("hello\n");

func ();
printf ("111111111111111111111111\n");

// 在停止进程之前,要检查文件的打开情况,并把文件缓冲区中的内容写回文件才停止进程


exit(0);
return 0;
}


int main14 ()
{

printf ("请选择文件: \n");
system ("ls /home");

char str[100];
while(1)
{
fgets(str, 100, stdin);
printf (" [root@localhost ldp3]#");
system(str);
}




return 0;
}


int main15()
{
printf ("hellp world\n");
sleep(4);

// 在内部fork()一个子进程,调用 /bin/sh -c string来执行
system("pstree");


printf ("aaaaaaaaaaaaaaaaaaaaa\n");
return 0;
}


int main16()
{
int count;

fork()||fork()||fork()||fork()||fork()&&fork();

count++;

printf("count=  d\n",count);

//while (1);

return 0;
}
int main18()
{
// 产生一个新的程序 ,替换了原有的程序,原有的进程id是不变的
// int ret = execl("/bin/ls", "ls", "-l", NULL);


int ret = execl("/usr/bin/pstree", "pstree", NULL);
if (ret == -1)
{
perror("execl");
return -1;
}



printf ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");

return 0;
}


int main17()//也就是相当于在终端上执行nixun了,执行方式加上执行的文件名,后面在加上一个空指针
{
int ret =execl("./nixun","nixun",NULL);

if (ret==-1)
{
perror ("execl");
return -1;
}


printf ("我是个大帅哥\n");
}




int main19()
{
int ret = execlp("/usr/bin/pstree", "pstree", NULL);
if (ret == -1)
{
perror("execl");
return -1;
}



printf ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
return 0;
}


int main20()//同上面的用法一样
{
//int ret = execlp("./nixun", "nixun", NULL);

// 错的int ret = execlp(" /usr/bin", "fuzhi1", NULL);

//int ret = execlp("./file", "file", NULL);

int ret = execlp("./nixun", "nixun", NULL);
if (ret == -1)
{
perror("execlp");
return -1;
}



printf ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
return 0;
}


int main21()//j加上两个被操作的文件就行了
{
char *a[] = {"nixun" ,NULL};
int ret = execv("nixun", a);
if (ret == -1)
{
perror("execl");
return -1;
}


return 0;
}
int main()//实现终端上的删除
{
// 产生一个新的程序 ,替换了原有的程序,原有的进程id是不变的
// int ret = execl("/bin/ls", "ls", "-l", NULL);
int ret = execl("nixun", "rm", "-rf", NULL);
if (ret == -1)
{
perror("execl");
return -1;
}



printf ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
return 0;
}







原创粉丝点击