作业1
1
之前就玩过虚拟机,所以直接vscode上ssh远程连接来写
没有或者不用vscode的话也可以直接在虚拟机上vim来写,只不过黑框框可能用得比较难受
2 sleep
1 2 3 4 5 6 7 8 9 10 11
   | #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h"
  int main(int argc,char **argv){     if(argc<2){         printf("usage: sleep <ticks>\n");     }     sleep(atoi(argv[1]));     exit(0); }
   | 
 
3 pingpong
单开一个管道的话,有可能我刚写进去就被自己读到了
所以开两个来进行操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   | #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h"
  signed main(int argc,char **argv){     int pp2c[2],pc2p[2];     pipe(pp2c);     pipe(pc2p);     if(fork()==0){         char buf;         read(pp2c[0],&buf,1);         printf("%d: received ping\n",getpid());         write(pc2p[1],&buf,1);     }     else{         write(pp2c[1], "!",1);         char buf;         read(pc2p[0], &buf,1);         printf("%d: received pong\n",getpid());         wait(0);     }     exit(0); }
 
   | 
 
4 prime
先写一个线性筛对比跑出来的结果
main塞完全部数字后塞入-1,如果读到-1就exit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   | #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h"
  const int N=36;
  #define N 36
  int a[N],n,st[N]; int id=1; void prime(){     for(int i=2;i<N;i++){         if(!st[i])a[++n]=i;         for(int j=1;a[j]*i<=N;j++){             st[a[j]*i]=1;             if(i%a[j]==0)break;         }     } }
  void dfs(int l[2]){     int p;     read(l[0],&p,sizeof p);     if(p==-1)exit(0);     printf("NO.%d prime is %d\n",id++,p);
      int r[2];     pipe(r);     if(fork()==0){         close(l[0]);         close(r[1]);         dfs(r);     }     else{         close(r[0]);         int buf;         while(read(l[0],&buf,sizeof (buf))&&id<n){             if(buf%p==0)continue;             write(r[1],&buf,sizeof (buf));         }         write(r[1],&buf,sizeof buf);         close(l[0]);         wait(0);         exit(0);     } }
  signed main(){     prime();     for(int i=1;i<=n;i++)printf("NO.%d :%d\n",i,a[i]);          printf("-----------------------------------\n");     int input_pipe[2];     pipe(input_pipe);     if(fork()==0){         close(input_pipe[1]);         dfs(input_pipe);         exit(0);     }     else{         close(input_pipe[0]);         for(int i=2;i<=35;i++){             write(input_pipe[1],&i,sizeof(i));         }                  int x=-1;         write(input_pipe[1],&x,sizeof (x));     }     wait(0);     exit(0); }
   | 
 
5 总结
这段时间ccpc预选赛和icpc预选赛所以很晚才开始写,不过之前在acwing上写过小项目所以对vim很熟悉(虽然很久很久没用过了)
在朋友的推荐下尝试了vscode来操作,确实更快而且更人性化
作业2