进程练习题

#include <mystdio.h>

int main(int argc, const char *argv\[\])

{

int fd_r =open("./2024.png",O_RDONLY);

if( fd_r <0)

{

printf("%d",LINE);

perror("open");

return -1;

}

int fd_w =open("./2024_fuben.png",O_WRONLY|O_CREAT|O_TRUNC,0777);

if(fd_w <0)

{

printf("%d",LINE);

perror("open");

return -1;

}

off_t size = lseek(fd_r,0,SEEK_END);

pid_t pid = fork();

if(pid>0)

{

sleep(4);

lseek(fd_r,0,SEEK_SET);

lseek(fd_w,0,SEEK_SET);

char c;

for(int i=0;i<size/2;i++)

{

read(fd_r,&c,1);

write(fd_w,&c,1);

}

printf("前半部分拷贝完毕\n");

}

else if(0 == pid)

{

char arr110,arr210,arr310;

sprintf(arr1,"%d",fd_r);

sprintf(arr2,"%d",fd_w);

sprintf(arr3,"%ld",size);

execl("b.out",arr1,arr2,arr3,NULL);

}

else{

perror("fork");

return -1;

}

close(fd_r);

close(fd_w);

return 0;

}

#include <mystdio.h>

int main(int argc, const char *argv\[\])

{

int fd_r = atoi(argv0);

int fd_w = atoi(argv1);

off_t size = atoi(argv2);

lseek(fd_r,size/2,SEEK_SET);

lseek(fd_w,size/2,SEEK_SET);

char c;

for(int i=0;i<size/2;i++)

{

read(fd_r,&c,1);

write(fd_w,&c,1);

}

printf("后半部分拷贝完毕\n");

return 0;

}

孤儿进程

#include <mystdio.h>

int main(int argc, const char *argv\[\])

{

pid_t pid = fork();

if(0 == pid)

{

while(1)

{

printf("%d %d\n",getppid(),getpid());

sleep(1);

}

}

return 0;

}

僵尸进程

#include <mystdio.h>

int main(int argc, const char *argv\[\])

{

pid_t pid=fork();

if(pid>0)

{

while(1)

{

printf("%d %d %d\n",getpid(),pid,LINE);

sleep(1);

}

}

else if(0 == pid)

{

for(int i=0;i<3;i++)

{

printf("%d %d %d\n",getppid(),getpid(),LINE);

sleep(1);

}

exit(0);

}

else{

perror("fork");

return -1;

}

return 0;

}

守护进程

#include <mystdio.h>

int main(int argc, const char *argv\[\])

{

pid_t pid=fork();

if(0 == pid)

{

//创建会话组

setsid();

//修改当前孤儿进程的运行目录

chdir("/usr");

//重设文件权限掩码

//关闭所有文件描述符

for(int i=0;i<getdtablesize();i++)

{

close(i);

}

while(1)

{

sleep(1);

}

}

return 0;

}

相关推荐
m0_747124534 小时前
MIPI 屏幕移植到不同 Linux 开发板步骤(以全志平台为例)
linux·嵌入式硬件·mipi
.道阻且长.5 小时前
Linux基础IO:系统文件IO
linux·运维·服务器
女神下凡7 小时前
芯易选(146):树莓派(Raspberry Pi)_开发指南
linux·arm开发·stm32·单片机·嵌入式硬件
by209998 小时前
Linux 软硬链接与文件删除机制
linux·服务器
我不会起名字3229 小时前
eBPF 与 perf 性能观测:On-CPU、Off-CPU 火焰图与内核探针的 4 个边界
linux·云原生·ebpf·perf·性能观测
俊俊几年的9 小时前
RK3588 开发板详解:硬件优势与嵌入式 Linux 学习路线
linux·c++·嵌入式
信安IT租赁10 小时前
小公司服务器用户与权限管理:chmod/chown/用户组从零理清
linux·运维·服务器
Kira Skyler12 小时前
linux /proc/kallsystem 构建与解析
linux
岁月蹉跎的一杯酒12 小时前
Linux V4L2 嵌入式相机驱动点亮
linux·数码相机·isp
wuminyu13 小时前
OpenJDK学习笔记:Native线程如何回调Java
java·linux·c语言·jvm·c++