c2c网站的功能,网页设计与编程,互联网营销软件,搭建一个平台目录
一、Linux操作系统
#xff08;1#xff09;第1关#xff1a;Linux初体验
#xff08;2#xff09;第2关#xff1a;Linux常用命令
#xff08;3#xff09;第3关#xff1a;Linux 查询命令帮助语句
二、Linux之进程管理—#xff08;重点#xff09;
1第1关Linux初体验
2第2关Linux常用命令
3第3关Linux 查询命令帮助语句
二、Linux之进程管理—重点
1第1关获取进程常见属性
2第2关进程创建操作-fork
3第3关进程创建操作-vfork
4第4关进程终止
三、生产者消费者问题实践
1第1关生产者消费者问题实践
2第2关进程互斥和同步
四、基于信号量的进程间通信
1第1关信号量IPC操作考查
五、基于命名管道与信号的进程间通信
1第1关命名管道与信号IPC操作考查
六、 Linux vi/vim编辑器难度简单
1第1关vi/vim基本用法
2第2关vi/vim工作模式切换
3第3关vi/vim命令模式
4第4关vi/vim底线命令模式重点
七、读文件系统函数难度简单
1第1关读文件系统函数
八、写文件系统函数难度中等
1第1关写文件系统函数 九、进程基础操作难度简单
1第1关进程基础操作考察 本篇博客的主要内容是在做大题时遇到的问题和一些总结。具体答案看我之前的博客 一、Linux操作系统 1第1关Linux初体验 2第2关Linux常用命令 将newfile文件复制一份到newdir目录下并命名为newfileCpy。 cp newfile newdir/newfileCpy 3第3关Linux 查询命令帮助语句 linux中使用man命令来查询命令的帮助文件 二、Linux之进程管理—重点 1第1关获取进程常见属性 获取进程本身的进程ID的系统调用函数是getpid()。获取父进程的进程ID的系统调用函数是getppid()。 2第2关进程创建操作-fork 这题需要注意的地方pid_t pid fork(); 3第3关进程创建操作-vfork 这题需要注意的地方pid_t pid vfork();子进程中输出Children字符串(提示需要换行) 4第4关进程终止 用这个注册函数atexit()和on_exit()调用成功返回0调用失败返回一个非零值。 void exit(){printf(%d,getpid());}if(atexit(exit)){} 三、生产者消费者问题实践 1第1关生产者消费者问题实践 观察题目代码给出的生产者函数。根据这个来写 sem_t empty; 类似等待一个信号(生产) sem_t full; 类似等待一个信号(消费) sem_wait(empty);sem_wait(full); 在每一个操作时和释放时都要记得加锁和解锁 加锁 pthread_mutex_lock(mutex);释放锁 pthread_mutex_unlock(mutex); 锁对象pthread_mutex_t mutex; 设置一个参数仿照生产者函数int nextc 0;先给一个等待消费条件sem_wait(full);再加锁pthread_mutex_lock(mutex); 意思是从之前生产了的东西拿东西出来消费nextcbuffer[out]; 与生产者函数中用到的一样int out 0; 这个跟着改printf(Consume one message:%d\n, nextc); fflush(stdout); //printf后请一定调用这句刷新输出缓存out (out 1) % SIZE; out (out 1) % SIZE; (这个跟生产者函数一样对着改就行) pthread_mutex_unlock(mutex); —— 再解锁 最后记得给一个消费完的信号sem_post(empty); 。意思其实就是消费完给一个叫空的信号。 具体的答案《消费Consumer()函数》 void *Consumer()
{// begin //请补充消费者线程函数代码int nextc 0;int i 0;for(; i 10; i){ int time rand() % 10 1;usleep(time*100000); sem_wait(full); pthread_mutex_lock(mutex);nextcbuffer[out];printf(Consume one message:%d\n, nextc);fflush(stdout);//printf后请一定调用这句刷新输出缓存out (out 1) % SIZE;pthread_mutex_unlock(mutex); //互斥锁解锁sem_post(empty);}// end }对比题目提供的《生产函数Producer()》 int in 0;
int out 0;
int buffer[SIZE];
sem_t empty;
sem_t full;
pthread_mutex_t mutex;void *Producer()
{int nextp 0;int i 0;for(; i 10; i){ int time rand() % 10 1;usleep(time*100000); sem_wait(empty); pthread_mutex_lock(mutex);buffer[in] nextc;printf(Produce one message:%d\n, nextp);fflush(stdout);//printf后请一定调用这句刷新输出缓存in (in 1) % SIZE;pthread_mutex_unlock(mutex); //互斥锁解锁sem_post(full);}
}2第2关进程互斥和同步 这个就简单了结合上面学的东西以及仿照这个题目女儿的苹果消费函数写就好 把消费等待的信号换成orange(桔子)把printf(....)输出换一下就好了 儿子消费函数答案 void *Son()
{// begin //请添加儿子线程的函数代码while(1){int time rand() % 10 1; //随机使程序睡眠0点几秒usleep(time * 100000); sem_wait(orange); pthread_mutex_lock(mutex);printf(儿子取了一个桔子\n) ;fflush(stdout);pthread_mutex_unlock(mutex); //互斥锁解锁sem_post(empty);}// end
} 四、基于信号量的进程间通信 1第1关信号量IPC操作考查 搞清楚函数semctl(int semid,int semnum,int cmd,...);semid要操作的信号量集合题目中给出变量int semid;semnum集合中信号量的编号题目中就是icmd执行的操作题目中有提示#define GETVAL 获取信号量的值返回信号的值 具体答案如下 此代码不严谨因为没有判断是否函数调用成功但是满足评测要求 for ( i 0 ; i MAX_SEMAPHORES ; i ){// begin /*请调用semctl函数读取并输出与上述数组输出相同的输出*/// 注意getval_arg.val 在这里不需要设置因为 GETVAL 不使用它 int x semctl(semid, i, GETVAL); printf(Semaphore %d, value %d\n, i, x); // end } 更完整、更严谨的答案记得还要在顶部导入包#include stdlib.h //以使用exit函数 for ( i 0 ; i MAX_SEMAPHORES ; i ){// begin /*请调用semctl函数读取并输出与上述数组输出相同的输出*/// 注意getval_arg.val 在这里不需要设置因为 GETVAL 不使用它 int x semctl(semid, i, GETVAL); if (x -1) { printf(GETVAL for semaphore %d failed (%d)\n, i, errno); exit(EXIT_FAILURE); }printf(Semaphore %d, value %d\n, i, x); // end } 五、基于命名管道与信号的进程间通信 1第1关命名管道与信号IPC操作考查 没啥好说的不会的操作系统考试就靠记呗 sleep(1); 休眠1秒打开读通道—— open(FIFO,O_RDONLY);打开写通道—— open(FIFO,O_WRONLY);开始写——write(fd,heool0penEuler,1); 答案如下 if(pid0){// begin /*子进程打开读管道随后关闭管道*/int fd;fd open(FIFO,O_RDONLY); /*rd-only 写*//*关闭*/close(fd);}else{/*父进程打开写通道休眠1秒尝试写入*/int fd;fd open(FIFO,O_WRONLY); /*wr-only 写*/int ret;sleep(1); /*休眠1秒*//*写入*/ret write(fd,heool0penEuler,1);// end } 六、 Linux vi/vim编辑器难度简单 注意这类题目在操作之前需要初始化任务环境 1第1关vi/vim基本用法 2第2关vi/vim工作模式切换 wq 保存并退出i插入 3第3关vi/vim命令模式 dd删除yy复制p粘贴 4第4关vi/vim底线命令模式重点 从第一行到最后一行寻找 word1 字符串并将该字符串取代为 word2。 :1,$s/word1/word2/g将文件第2-5行内容另存为oldFileCpy.txt文件2,5 w oldFileCpy.txt。 七、读文件系统函数难度简单 1第1关读文件系统函数 (此题注意知道read()函数是啥就行了。其次知道其返回值和其函数的参数) 函数原型read(int fd, void *buf, size_t count);fd看上文(读文件的地址)*buf字符数组缓冲区的位置最后这个size_t count就是读到的全部字符的个数—— sizeof(buffer)-1 #include fcntl.h
#include stdio.h
#include stdlib.h
#define rwmode 0
int main()
{int fd;char buffer[1024];int n;fd open(/data/workspace/myshixun/case1/testFIle, rwmode);if (fd 0){printf(Open file error!\n);exit(1);}elseprintf(open testFIle ok!\n);//-------------begin---------------------- //请使用read函数将其读入buffer中n read(fd,buffer,sizeof(buffer)-1);//--------------end---------------------- buffer[n] \0;printf(%s\n, buffer);close(fd);return 0;
}八、写文件系统函数难度中等 1第1关写文件系统函数 这题其实就是一个综合。先用read()读函数再用write()写函数 函数原型ssize_t write(int fd, const void *buf, size_t count); 读的文件的地址题目也给出int resource_fd char buffer[FILESIZE]字符数组 #define FILESIZE 1024 (该字符数组最大容量也是题目要求读取的量)所以(read(resource_fd,buffer,FILESIZE ))0因为读到了就要返回得到的个数。 char buffer[FILESIZE], *p; p buffer; 要写入的字符数组信息地址在p 写的目的文件地址题目也给出了int destination_fd 具体要写的字符个数(前面被赋值)readbytes read(resource_fd,buffer,FILESIZE) 所以(writebytes write(destination_fd,p,readbytes))0因为写到了就要返回个数 综合解答 #include fcntl.h
#include stdio.h
#include stdlib.h
#include errno.h
#define resource_mode 0
#define destination_mode 0774
#define FILESIZE 1024int main(int argc, char *argv[])
{int resource_fd, destination_fd;char buffer[FILESIZE], *p;int readbytes, writebytes;if (argc ! 3){printf(Usage:copy from resource file to destination file\n %s src_file dest_file\n, argv[0]);exit(0);}if ((resource_fd open(argv[1], resource_mode)) -1){perror(Cant open source file);exit(0);}if ((destination_fd creat(argv[2], destination_mode)) -1){perror(Cant create destination file);exit(0);}// begin // 请使用read函数读取前1024字节的内容读到缓冲区buffer中while ((readbytes read(resource_fd,buffer,FILESIZE))0)// end {p buffer;if ((readbytes -1) (errno ! EINTR))break;else if (readbytes 0){// begin // 请使用write函数读取到的前1024字节的内容写到目的文件中while ((writebytes write(destination_fd,p,readbytes))0)// end {if ((writebytes -1) (errno ! EINTR))break;else if (writebytes readbytes)break;else if (writebytes 0){p writebytes;readbytes - writebytes;}}if (writebytes -1)break;}}close(resource_fd);close(destination_fd);return 0;
}九、进程基础操作难度简单 1第1关进程基础操作考察 这一关没啥好说的难度很简单 了解fork()函数即可注意空格的输出父、子进程都一样printf(bye! ); #includestdio.h
#includeunistd.h
#includefcntl.h
#includestdlib.h
int main(int argc,char *argv[])
{/*请开始填写*/pid_t pid fork();if(pid0){printf(bye! );}else if(pid0){printf(bye! );}return 0;
}