GYFLinux编程实验

创建文件file1,写入字符串“abcdefghijklmn”; 创建文件file2,写入字符串“ABCDEFGHIJKLMN”;读取file1中的内容,写入file2,使file2中的字符串内容为“ ABCDEFGHIJKLMNabcdefghijklmn”

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>

int main() {
	int fd1,fd2;

	fd1=open("file1",O_CREAT|O_RDWR,S_IRWXU);
	if(fd1< 0) {
		perror("open :");
		printf("errno is:%d \n",errno);
	} else
		printf("open ok\n");

	fd2=open("file2",O_CREAT|O_RDWR,S_IRWXU);
	if(fd2< 0) {
		perror("open:");
		printf("errno is:%d \n,errno");
	} else
		printf("open OK \n");

	int fdw1,fdw2;

	fdw1=write(fd1,"abcdefghijklmn",15);
	printf("fdw1:%d\n",fdw1);
	if(fdw1!=15) {
		perror("write fd1:");
	} else
		printf("write OK\n");
	lseek(fd2,16,SEEK_SET);
	char buf[20]="ABCDEFGHIJKLMN";
	fdw2=write(fd2,buf,20);
	if(fdw2<0)
		perror ("write fd2:");
	else
		printf("write OK\n");
	lseek (fd1,0,SEEK_SET);
	lseek(fd2,0,SEEK_SET);
	char re[28];
	read(fd1,re,14);
	write(fd2,re,14);
	close(fd1);
	close(fd2);
	return 0;
}

创建新文件,该文件具有用户读写权限。采用dup/dup2/fcntl复制一个新的文件描述符,通过新文件描述符向文件写入“class_name”字符串;通过原有的文件描述符读取文件中的内容,并且打印显示;

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

int main() {
	int fd1,fd2;
	fd1=open("file",O_RDWR|O_CREAT,0644);
	fd2=dup(fd1);

	char str[30];
	char name[30]="class_name";
	lseek(fd2,0,SEEK_SET);
	write(fd2,name,30);
	lseek(fd1,0,SEEK_SET);
	read(fd1,str,30);
	printf("%s",str);
	close(fd1);
	return 0;
}


实验三: 编写代码实现以下功能: 1.打印字符串“hello world!” 2.在打印字符串“hello world!”前调用三次fork,分析打印结果。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int i;
for(i=0;i<3;i++)
{
fork(); 
}
printf("hello world!!!\n");
return 0;
}

编写程序完成以下功能: 1.递归遍历/home目录,打印出所有文件和子目录名称及节点号。 2.判断文件类型,如果是子目录,继续进行递归遍历,直到遍历完所有子目录为止.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
int show (char * path)
{
char p[500];
DIR *dir;
struct stat statbuf;
struct dirent *dire;
lstat (path,&statbuf);
if (S_ISDIR(statbuf.st_mode))
{
dir = opendir (path);
if (dir)
{
while( (dire = readdir(dir) ) !=NULL)
{
if(( dire ->d_name[0] )=='.')
continue;
sprintf(p,"%s/%s",path,dire->d_name);
lstat(p,&statbuf);
printf ("\t该目录文件名为: %s \n",p);
printf ("\t该目录文件节点号为: %ld \n",statbuf.st_ino);
show (p);
}
}
}
if (S_ISREG(statbuf.st_mode)){
printf ("该文件名为: %s \n",path);//输出文件名
printf ("该文件节点号为: %ld \n",statbuf.st_ino);
}
}
int main()
{
show("/home");
return 0;
}

在子进程中打开文件file1,写入自己的“班级_姓名_学号”, 2.父进程读取file1中的内容,并且打印显示。 3.在父进程中获取已经结束的子进程的状态信息,打印该信息,并且打印结束的子进程的进程号。

#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]= "zhinengsanban_liaoxianqiang_1915925583";
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;
}

利用匿名管道实现父子进程间通信,要求父进程发送字符串“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;
}

利用匿名管道实现兄弟进程间通信,要求兄进程发送字符串“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");
		}
	}
	return 0;
}

编写程序实现以下功能: 利用有名管道文件实现进程间通信,要求 写进程向有名管道文件写入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");
}
}

在父进程中定义变量 n,在子进程中对变量 n 进行++操作;并且打印变量 n 的值,打印子 进程 pid; 在父进程中打印变量 n 的值,并且打印父进程 pid。 要求分别用 fork 和 vfork 创建子进程。

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值