Linux编程基础(第二章作业)

一、解释shell命令

1、gcc hello.c -o hello:用gcc编译器编译hello.c源文件,生成名为hello的可执行文件。
2、./hello:执行当前文件夹下的hello文件
3、gdb hello:启动gdb调试hello程序

二、解释程序中指定的句子的功能

1、$(CC) $^ -o $@

将所有不重复的依赖文件编译编译成可执行文件,以目标文件的完整名称命名。

2、fd=open(“temp.log”,O_RDONLY);

以只读方式打开当前文件夹下的temp.log文件。

size=read(fd,buffer,sizeof(buffer));

将读取到的文件内容保存到buffer指定的字符串数组中,返回读取的字符个数。

3、FILE*fp=fopen(“recfile”,“w”);

定义一个名为fp的FEIL类型指针,用来调用fopen()函数,以只写方式打开当前目录下的recfile文件,如果打开成功,将文件起始位置地址返回给fp指针,否则返回NUll。

4、execlp(“ps”,“ps”,"-ef",NULL);

execlp()从PATH 环境变量所指的目录中查找ps命令程序并执行,以全格式查看当前所有进程。

5、pid_w=waitpid(pid,NULL,WNOHANG);

父进程以非阻塞方式等待子进程结束,若有子进程退出,则waitpid返回子进程识别码pid;若没有子进程推出,则waitpid返回0。

6、int ret = mkfifo(write_fifo_name,S_IRUSR | S_IWUSR);

创建名为write_fifo_name的fifo有名管道文件,一个进程拥有读权限,一个进程拥有写权限。

三、使用C语言编程完成以下功能

(害,就是书上的程序!)

(1)使用fork创建进程;
子进程使用基本IO函数创建可读可写文件test.txt,写入“姓名+学号”字符串;
父进程从test.txt读出文件内容,并显示出来。最后调用wait清理子进程。

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

int main(void)
{
        int fd,size;
        char s[]="姓名+学号\n";
        char buffer[40];
        pid_t pid,pid_w;
        pid=fork();
        if(pid < 0)
        {
                perror("error in fork!\n");
                exit(1);
        }
        if(pid == 0)
        {
                /*可写方式打开一个文件,如果不存在则创建该文件*/
                fd=open("test.txt",O_WRONLY|O_CREAT,S_IRWXU);
                if(0 == fd)
                {
                        printf("Open or creat test.txt failed.\n");
                        return -1;
                }
                /*向该文件写入字符串*/
                write(fd,s,sizeof(s));
                close(fd);
        }
        else
        {       
        	if(pid_w==0)
        	{
        		pid_w=wait(NULL);
        	}        
                fd = open("test.txt",O_RDONLY,S_IRWXU);
                if(-1 == fd)
                {
                	printf("failed!\n");
                	return -1;
                }
                size = read(fd,buffer,sizeof(buffer));
                close(fd);
                printf("%s",buffer);
                
                pid_w=wait(NULL);
                return 0;
         }
}

(2)父进程通过匿名管道向子进程写入“姓名+学号”字符串,子进程从管道读取数据并显示。

#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
int main()
{
        int fd[2],nbytes;
        pid_t childpid;
        char string[]="姓名+学号\n";
        char readbuffer[100];
        
        if(pipe(fd)< 0)
        {
                printf("build failed\n");
                return -1;
        }
        if((childpid=fork())== -1)
        {
                perror("fork");
                exit(1);
        }
        if(childpid == 0)
        {       close(fd[0]);   
                sleep(3);
                write(fd[1],string,strlen(string));
                printf("%s",readbuffer);
                close(fd[1]);   
                exit(0);
        }
        else
        {
                close(fd[1]);   
                nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
                printf("%s",readbuffer);
                close(fd[0]);   
        }
        return (0);
}

(3)Makefile我把两个c文件同时编译的,只写一个程序的话删掉多余的就可^ - ^

#makefile
CC=gcc
all:homework3_2 homework3_1
.PHONY:all 

homework3_2 :ujung/homework3_2.o 
	$(CC) -o $@ $<
homework3_1 :ujung/homework3_1.o 
	$(CC) -o $@ $<
yue/homework3_2.o:ujung/homework3_2.c
	$(CC) -o $@ -c $<
yue/homework3_1.o:ujung/homework3_1.c
	$(CC) -o $@ -c $<

clean:
	rm -rf homework3_2 homework3_1 ujung/homework3_2.o ujung/homework3_1.o
	
.PHONY:all clean
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值