bcbcbc

1.创建文件 file1,写入字符串“abcdefghijklmn”;
创建文件 file2,写入字符串“ABCDEFGHIJKLMN”;
读取file1中的内容,写入file2,使file2中的字符串内容为“abcdefghijklmn ABCDEFGHIJKLMN” 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
int fd1,fd2;
char str[14];
fd1 = open("file1",O_RDWR|O_CREAT,S_IRWXU);
if(fd1 < 0)
perror("open");
write(fd1,"abcdefghijklmn",14);
lseek(fd1,0,SEEK_SET);
fd2 = open("file2",O_RDWR|O_CREAT,S_IRWXU);
if(fd2 < 0)
perror("open");
lseek(fd2,14,SEEK_END);
write(fd2,"ABCDEFGHIJKLMN",14);
read(fd1,str,14);
lseek(fd2,0,SEEK_SET);
write(fd2,str,14);
close(fd1);
close(fd2);
system("cat file2");
printf("\n");
return 0;
}
2.创建新文件,该文件具有用户读写权限。
采用 dup/dup2/fcntl 复制一个新的文件描述符,通过新文件描述符向文件写入“class_name”
字符串;
通过原有的文件描述符读取文件中的内容,并且打印显示
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int fd,fd1;
char *str = "class_name";
fd = open("file",O_CREAT|O_RDWR);
if(fd < 0)
 perror("open");
fd1 = dup(fd);
if(fd1 < 0)
 perror("dup");
write(fd1,str,strlen(str));
lseek(fd,0,SEEK_SET);
char buf[12];
read(fd,buf,12);
printf("The buf is:%s\n",buf);
close(fd);
close(fd1);
return 0;
}
3.递归遍历/home 目录,打印出所有文件和子目录名称及节点号。
判断文件类型,如果是子目录,继续进行递归遍历,直到遍历完所有子目录为止。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
void show(char *path)
{
DIR *dir;
char str[128];
struct dirent *dirp;
struct stat statbuf;
dir = opendir(path);
if(dir)
{
while((dirp = readdir(dir)) != NULL)
{
sprintf(str,"%s/%s",path,dirp->d_name);
if(lstat(str,&statbuf) < 0)
 perror("lstat");
if(dirp->d_name[0] == '.')
 continue;
if(S_ISDIR(statbuf.st_mode))
{
show(str);
printf("The dirent's name is: %s\n",dirp->d_name);
printf("The dirent's inode is: %d\n",dirp->d_ino);
}
else
{
printf("The file's name is: %s\n",dirp->d_name);
printf("The file's inode is: %d\n",dirp->d_ino);
}
}
}
else
perror("opendir");
closedir(dir);
}
int main()
{
show("/home");
return 0;
}
4.打印字符串“hello world!”
在打印字符串“hello world!”前调用三次 fork,分析打印结果。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello world!!!\n");
return 0
}
5.创建子进程
在子进程中打开文件 file1,写入自己的“班级_姓名_学号”,
父进程读取 file1 中的内容,并且打印显示。
在父进程中获取已经结束的子进程的状态信息,打印该信息,并且打印结束的子进程的进程
号。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
int main()
{
int fd,pid;
fd = open("file",O_CREAT|O_RDWR,S_IRWXU);
if(fd< 0)
 perror("open");
pid = fork();
if(pid == 0)
{
printf("This is the child!\n");
char str[128] = "XXXX_XXXXX_XXXXXXXXXXX";
if(write(fd,str,128) < 0)
 perror("write");
exit(5);
}
else
{
printf("This is the father!\n");
char buf[128];
int n,status;
if(read(fd,buf,128) < 0)
 perror("read");
printf("The buf is: %s\n",buf);
if(wait(&status) < 0)
 perror("perror");
if(WIFEXITED(status))
 n = WEXITSTATUS(status);
else
 printf("wait error!\n");
printf("The child's pid is: %d\n",pid);
printf("The child exit status is: %d\n",n);
}
return 0;
}
6.在父进程中定义变量 n,在子进程中对变量 n 进行++操作;并且打印变量 n 的值,打印子
进程 pid;
在父进程中打印变量 n 的值,并且打印父进程 pid。
要求分别用 fork 和 vfork 创建子进程。
//------fork
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int n = 1;
if(fork() == 0)
{
printf("This is child,the pid is%d\n",getpid());
printf("The n is: %d\n",++n);
}
else
{
printf("This is father,the pid is%d\n",getpid());
printf("The n is: %d\n",n);
}
return 0;
}
//-----vfork
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int n = 1;
pid_t pid;
pid = vfork();
if(pid < 0)
 perror("vfork");
else if(pid == 0)
{
printf("This is child,the child's pid is: %d\n",getpid());
printf("The n is: %d\n",++n);
exit(0);
}
else
{
printf("This is father,the father's pid is: %d\n",getpid());
printf("The n is: %d\n",n);
}
return 0;
}
7.利用匿名管道实现父子进程间通信,要求
父进程发送字符串“hello child”给子进程;
子进程收到父进程发送的数据后,给父进程回复“hello farther”;
父子进程通信完毕,父进程依次打印子进程的退出状态以及子进程的 pid。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int fd1[2],fd2[2];
pipe(fd1);
pipe(fd2);
int pid;
pid = fork();
if(pid < 0)
perror("fork");
else if(pid == 0)
{
close(fd1[0]);
close(fd2[1]);
char str[12];
printf("This is the child!\n");
if(read(fd2[0],str,12) > 0)
{
printf("Received the news: %s\n",str);
if(write(fd1[1],"hello father",12) < 0)
 perror("write");
}
else
perror("read");
exit(5);
}
else
{
int status;
printf("This is the father!\n");
close(fd1[1]);
close(fd2[0]);
char buf[24] = "hello child";
if(write(fd2[1],buf,12) < 0)
 perror("write");
else
{
printf("Send news successful!\n");
}
wait(&status);
if(WIFEXITED(status))
{
printf("The child's pid is: %d\n",pid);
printf("The child's exited status is: %d\n",WEXITSTATUS(status));
}
}
return 0;
}
8. 利用匿名管道实现兄弟进程间通信,要求
兄进程发送字符串“This is elder brother ,pid is (兄进程进程号)”给第进程;
第进程收到兄进程发送的数据后,给兄进程回复“This is younger brother ,pid is(第进程进
程号)”;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main()
{
int fd1[2],fd2[2];
pipe(fd1);
pipe(fd2);
int pid;
pid = fork();
if(pid == 0)
{
printf("This is the elder brother!\n");
printf("The elder's father's pid is: %d\n",getppid());
close(fd1[1]);
close(fd2[0]);
char str1[64],str2[64];
sprintf(str1,"This is the elder brother,pid is %d",getpid());
if(write(fd2[1],str1,64) < 0)
 perror("write");
if(read(fd1[0],str2,64) < 0)
 perror("read");
else
 printf("The news from younger is: %s\n",str2);
}
else
{
if(fork() == 0)
{
printf("This is the younger brother!\n");
printf("The younger's father's pid is: %d\n",getppid());
close(fd1[0]);
close(fd2[1]);
char buf1[64],buf2[64];
if(read(fd2[0],buf1,64) > 0)
{
printf("The news form elder is: %s\n",buf1);
sprintf(buf2,"This is the younger brother,pid is %d",getpid());
if(write(fd1[1],buf2,64) < 0)
perror("write");
}
else
 perror("read");
}
}
}
9. 利用有名管道文件实现进程间通信,要求
写进程向有名管道文件写入 10 次“hello world”;
读进程读取有名管道文件中的内容,并依次打印
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int main()
{
int pid,fd;
if(mkfifo("fifotest",0666) < 0)
 perror("mkfifo");
pid = fork();
if(pid < 0)
 perror("fork");
else if(pid == 0)
{
printf("This is the write process!\n");
int fd = open("fifotest",0666);
for(int i = 0; i < 10;i++)
{
if(write(fd,"hello world",12) < 0)
perror("write");
sleep(1);
}
close(fd);
}
else
{
char str[128];
printf("This is the read process!\n");
int fd1 = open("fifotest",0666);
for(int i = 0;i < 10;i++)
{
if(read(fd1,str,128) < 0)
 perror("read");
else
 printf("%s\n",str);
}
system("rm -f fifotest");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值