linux:实训及部分代码

项目一第一题
1、makefile文件编写
实验内容:程序功能,主函数通过调用函数isPrime(num)判定num是否素数,
并生成一个100个元素为素数的数组,然后调用函数outprime(int a[],int n)输出n个素数
。通过编写一个makefile文件,实现三个模块的C语言程序编译运行。
findprime.c

#include "findprime.h"
int main(){
int a[N],count=0,num=2;
count=0;
while(count<100){
if(isprime(num)){
a[count]=num;
count++;}
num++;
}
printf("% primes:\n",count);
outprime(a,count);
return 0;
}

findprime.h

#include <stdio.h>
#define N 100
int isprime(int num);
void outprime(int a[],int n);

Makefile

#Makefile
findprime:findprime.o myproc1.o myproc2.o
    gcc -lm -o findprime findprime.o myproc1.o myproc2.o
    mv -f findprime "$HOME/bin"
    findprime
findprime.o:findprime.c findprime.h
    gcc -c findprime.c
myproc1.o:myproc1.c
    gcc -c myproc1.c
myproc2.o:myproc2.c
    gcc -c myproc2.c
clean:
    rm "$HOME/bin/findprime" myproc1.o myproc2.o findprime.o

myproc1.c

#include <stdio.h>
#include <math.h>
int isprime(int num){
int k;
int n;
n=(int)sqrt(num);
for(k=2;k<=n&&num%k!=0;k++);
if(k>n)
return 1;
else return 0;
}

myproc2.c

#include <stdio.h>
void outprime(int a[],int n){
int i;
for(i=0;i<n;i++){
printf("%5d",a[i]);
if((i+1)%10==0)printf("\n");
}}

项目一第二题
读写文件与进程进程管理系统调用综合编程
myproc3.c

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
extern int errno;
int main(){
char buf[100];
pid_t cld_pid,pt_pid;
int fd,status;
if((fd=open("temp",O_CREAT|O_TRUNC|O_RDWR,S_IRWXU))==-1){
  printf("open error %d \n",errno);
  exit(1);
}
strcpy(buf,"This is parent process write");
pt_pid=getpid();
if((cld_pid=fork()==0)){  //子进程
strcpy(buf,"This is child process write");
printf("My PID(child) is %d \n",getpid());
printf("My parent PID is %d \n",pt_pid);
write(fd,buf,strlen(buf));
close(fd);
exit(0);
}
else{//父进程
printf("This parent process\n");
printf("My PID(parent) is %d\n",getpid());
printf("My child PID is %d\n",cld_pid);
write(fd,buf,strlen(buf));
close(fd);
}
wait(&status);
return 0;
}

项目一第三题
(1)自行编写一个C语言程序,在主进程中生成两个子进程,
在子进程1中将“1111111111”、“2222222222”、……、“9999999999”写入文件myfile,
在子进程2中将“AAAAAAAAAA”、“BBBBBBBBBB”、……、“ZZZZZZZZZZ”写入文件myfile。

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(){
   pid_t cid,ppid;
   char buf;
   int in,i,j;
if (cid=fork()!=-1)
        { in=open("myfile",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);       
 for (i=0;i<9;i++){
          for(j=0;j<10;j++){
          buf ='1'+i;
          write(in,&buf,1);
          }
        write(in, "\n", 1);
        }
   }
   if(ppid!=-1){
        for (i=0;i<26;i++){
          for (j=0;j<10;j++){
          buf = 'A'+i;
          write(in,&buf,1);
          }
    write(in,"\n",1);
        }
       close(in);
   }
}    

项目一第四题
编制并调试一个多进程程序,主进程中通过生成2个子进程,其中子进程1
调用gen_prime()生成从2开始的素数,存入共享内存,进程2调用out_num()
从共享内存取出数据显示输出,每行输出10个数。注意两个进程同步问题,
通过使用信号量sem_t类型变量实现同步互斥。
为了简化程序,也可以采用调用sleep(5)让子进程2先延迟5秒。

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <math.h>
#define MAXSIZE 1000
int isprime(int num){
    int k;
    int n;
    n=(int)sqrt(num);
    for(k=2;k<=n&&num%k!=0;k++);
    if(k>n)
        return 1;
    else
        return 0;
}

int gen_prime(int *p){
    int num=2,len=0;
    while(num<MAXSIZE){
        if(isprime(num)){
            *p=num;
            p++;
            len++;
        }
    num++;
    }
return len;
}

void out_number(int *p,int n){
    int i,j;
    for(i=0;i<n;i++){
        printf("%d ",*p);
        if(i%10==0)
            printf("\n");
        p++;
    }
}

int main(){
    pid_t pid;
    int *p,*num;
    int shmid,shmidn;
    shmid=shmget(IPC_PRIVATE,MAXSIZE,IPC_CREAT|0600);
     if(shmid<0){
        printf("get shm ipc_id error!\n");
        exit(1);
    }
    p=(int *)shmat(shmid,NULL,0);
    if((int)*p==-1){
            printf("shmat addr error!\n");
            exit(1);
    }
    shmidn=shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0600);
    if(shmidn<0){
        printf("get shm ipc_id error!\n");
        exit(1);
    }
    num=(int *)shmat(shmid,NULL,0);
    if((int)*num==-1){
        printf("shmat addr error! \n");
        exit(1);
    }
    pid=fork();
    if(pid==0){
        *num=gen_prime(p);
        exit(0);
    }
    pid=fork();
    if(pid==0){
        sleep(5);
        out_number(p,*num);
        exit(0);
    }
    sleep(10);
    exit(0);
}

项目二方法四
1)创建1个组群账户,取名stugrp,设置GID=610;
(2)批量创建50个用户stu001,stu002,…,stu050,设置其UID=510,511,512,…,559。并设置GID=610,设置用户主目录为/home/stu001,… ,/home/stu050,
设置shell为/bin/bash。

adduser.sh

#/bin/bash
uid=500
gid=610
for((i=1;i<$51;i++))
do
if [ "$i" -lt 10 ]
then
uname=stu0$i
else
uname=stu$i
fi
    let "uid=uid+1"
    echo $uname:x:$uid:$gid:$uname:/home/$uname:/bin/bash>>user.txt
    echo $uname:stu123>>userpw.txt
done

项目三
编写一个显示菜单的shell程序,利用函数实现简单的菜单功能,n的值由键盘输入:
===========================================**
(1)计算1到n的奇数之和;
(2)计算1到n的阶乘;
(3)计算1到n的所有素数;
(4) 退出

==========================================
Please enter function select and number: 1 1000

#/bin/bash
function method1(){
    sum=0
    for((i=1;i<=$y;i++,i++))
    do
        let "sum=sum+i"
    done
    echo "1 add to n sum is $sum"
}

function method2(){
    sum1=1
    sum2=0
    for((i=1;i<=$y;i++))
    do
        let "sum1=sum1*i"
    done
    echo "1 to n allsum is $sum1"
}

function method3(){
    for((i=2;i<$y;i++))
    do
        isprime
        if [ "$?" -eq 0 ]
            then echo -n "$i "
        fi
    done
    echo
}

function isprime(){
    if((i==2))
        then return 0
    elif((i%2==0))
        then
    return 1    
    fi
        bond=$((i/2));flag=0
    for((k=3;k<=bond;k=k+2))
    do
        if((i%k==0));then
            flag=1
            break
        fi
    done
return $flag
}

#主函数 输入x y  如果x为4退出循环
x=0
for((;$x!=4;))
do
    echo ===========================================
    echo "**    (1)计算1到n的奇数之和;       **"
    echo "**    (2)计算1到n的阶乘;           **"
    echo "**    (3)计算1到n的所有素数;       **"
    echo "**    (4)退出程序。                 **"
    echo ==========================================
    echo -n Please enter function select and number:
    read x y
    case $x in
        1) method1 ;;
        2) method2 ;;
        3) method3 ;;
    esac
done
  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值