进程与控制
来源:互联网 发布: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;
}
阅读全文
0 0
- 进程环境与进程控制
- 进程控制与进程关系
- 进程控制与进程通信
- 进程控制与调度
- 进程描述与控制
- 进程与控制
- LInux进程控制与编程
- LInux进程控制与编程
- linux--进程管理与控制
- Linux进程控制与程序设计
- 【操作系统】进程描述与控制
- 进程的描述与控制
- 进程的描述与控制
- 进程控制与进程通信编程
- UNIX进程环境与进程控制
- 进程环境与进程控制(5): 进程时间
- 进程服务编写,与启动停止控制
- 第二章 进程的描述与控制
- Android动态获取权限
- Qt之QTcpServer/QTcpSocket简单收发信息
- 信用是个什么玩意
- icepdf去水印方法
- Linux 发送get和post请求测试接口
- 进程与控制
- 有权访问另一个函数作用域内变量的函数都是闭包。
- Java8内存模型—永久代(PermGen)和元空间(Metaspace)
- 三、序列式容器
- ssh 免密码设置失败原因总结
- UVA-624 记录路径dp
- dos命令查找mysql数据库
- MD5 32位加密
- 翻转单词顺序VS左旋转字符串