Linux操作系统实践中的实验(1)

本文深入探讨了系统调用的基础,如获取进程ID、用户ID等,并通过实例展示了如何使用C语言进行进程控制,包括创建子进程、发送信号终止进程。此外,文章还介绍了多线程编程,包括线程创建、线程间通信以及使用互斥锁实现资源的同步访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先是简单的系统调用,类似于当前的进程号,组号,用户名等等

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <stdio.h>
int main(int argc, char **argv){
    pid_t my_pid, parent_pid;
    uid_t my_uid, my_euid;
    gid_t my_gid, my_egid;
    struct passwd *my_info;
    my_pid = getpid();          //process id
    parent_pid = getppid();     //parent process id
    my_uid = getuid();          //user id
    my_euid = geteuid();        //effective user id
    my_gid = getgid();          //group id
    my_egid = getegid();        //effective group id
    my_info = getpwuid(my_uid); //user information
    printf("Process ID:%1d\n", my_pid);
    printf("Parent ID:%1d\n", parent_pid);
    printf("User ID:%1d\n", my_uid);
    printf("Effective User ID:%1d\n", my_euid);
    printf("Group ID:%1d\n", my_gid);
    printf("Effective group ID:%1d\n", my_egid);
    if(my_info){
        printf("My login name:%s\n", my_info->pw_name);
        printf("My password:%s\n", my_info->pw_passwd);
        printf("My user ID:%1d\n", my_info->pw_uid);
        printf("My group ID:%1d\n", my_info->pw_gid);
        printf("My real name:%s\n", my_info->pw_gecos);
        printf("My home dir:%s\n", my_info->pw_dir);
        printf("My work shell:%s\n", my_info->pw_shell);
    }
    return 0;
}

接下来是进程的基本操作,比如kill

#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main(void){
    pid_t childpid;
    int status;
    int retval;
    childpid = fork();      //clone a new process
    printf("childpid=%d\n", childpid);
    if(childpid == -1){     //fail to create a new process
        perror("fork()");
        exit(EXIT_FAILURE);
    }
    else if(childpid == 0){
        puts("In child process\n");
        printf("pid=%d, sleep 100 sec\n", (int)getpid());
        sleep(100);
        exit(EXIT_SUCCESS);
    }
    else{
        printf("ppid=%d, wait the end of childpid, if it does not end, exit immediately\n", (int)getpid());
        if(waitpid(childpid, &status, WNOHANG)==0){
            //pid does not end
            printf("kill the pid\n");
            retval = kill(childpid, SIGKILL);
            printf("retval=%d\n", retval);
            printf("ppid killed the pid\n");
            if(retval){
                puts("kill failed.\n");
                perror("kill");
                waitpid(childpid, &status, 0);
            }
            else{
                printf("%d killed\n", childpid);
                printf("after killing the pid, the living processes now\n");
            }
        }
    }
    exit(EXIT_SUCCESS);
    return 0;
}

然后是简单的多线程操作

#include <pthread.h>
#include <stdio.h>
/*
*  you should add -lpthread while compiling this file
*  like gcc multiThreads.c -o multiThreads.out -lpthread
*/
void *printyou(void* unused){
    int c=20;
    while(c--)  printf("11  ", stderr);
    return NULL;
}
void *printme(void* unused){
    int c=20;
    while(c--) printf("22  ", stderr);
    return NULL;
}
void *printhim(void* unused){
    int c=20;
    while(c--) printf("33  ", stderr);
    return NULL;
}
int main(){
    int c=20;
    pthread_t t1, t2, t3;
    //create new thread
    /*
    *   param1 thread identifier
    *   param2 the attribute of the thread
    *   param3 the starting addr of function
    *   param4 the running attribute
    */
    pthread_create (&t1, NULL, &printyou, NULL);
    pthread_create (&t2, NULL, &printme, NULL);
    pthread_create (&t3, NULL, &printhim, NULL);
    while(c--) printf("44  ", stderr);
    return 0;
}

不知道为什么每次结果都不太一样。。

最后是有锁保护的多线程访问理解资源

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
int num=20,count=3;
pthread_mutex_t mylock=PTHREAD_COND_INITIALIZER;
void *sub1(){
    int i=0,tmp;
    for(;i<count;i++){
        pthread_mutex_lock(&mylock);
        printf("t1: before sub 1 num=%d\n", num);
        tmp = num-1;
        printf("t1: after sub 1 tmp=%d\n", tmp);
        num = tmp;
        printf("t1: after sub 1 num=%d\n", num);
        pthread_mutex_unlock(&mylock);
        sleep(1);
    }
}
void *sub2(){
    int i=0,tmp;
    for(;i<count;i++){
        pthread_mutex_lock(&mylock);
        printf("t2: before sub 1 num=%d\n", num);
        tmp = num-1;
        printf("t2: after sub 1 tmp=%d\n", tmp);
        num = tmp;
        printf("t2: after sub 1 num=%d\n", num);
        pthread_mutex_unlock(&mylock);
        sleep(1);
    }
}
int main(int argc, char **argv){
    pthread_t t1, t2;
    int err, i=0, tmp;
    void *tret;
    err = pthread_create(&t1, NULL, sub1, NULL);
    if(err!=0){
        printf("t1 create error:%s\n", strerror(err));
        exit(-1);
    }
    err = pthread_create(&t2, NULL, sub2, NULL);
    if(err!=0){
        printf("t2 create error:%s\n", strerror(err));
        exit(-1);
    } 
    for(;i<count;i++){
        pthread_mutex_lock(&mylock);
        printf("main: before sub 1 num=%d\n", num);
        tmp = num-1;
        printf("main: after sub 1 tmp=%d\n", tmp);
        num = tmp;
        printf("main: after sub 1 num=%d\n", num);
        pthread_mutex_unlock(&mylock);
        sleep(1);
    }
    printf("two threads end\n");
    err = pthread_join(t1, &tret);   //block the thread
    if(err!=0){
        printf("can not join the thread t1:%s", strerror(err));
        exit(-1);
    }
    printf("t1 exit code:%d\n", (int)tret);
    err = pthread_join(t2, &tret);   //block the thread
    if(err!=0){
        printf("can not join the thread t2:%s", strerror(err));
        exit(-1);
    }
    printf("t2 exit code:%d\n", (int)tret);
    return 0;
}

一、 实验目的 1、掌握在虚拟机环境下安装 Linux系统 2、熟练掌握基本 shell命令 3、熟练掌握shel1高级操作 二、 开发工具和运行环境 Vmware15虚拟机,Centos7的iso镜像文件 三、 实验内容 1、在虚拟机环境下安装 CentOS7操作系统 如果有虚拟机环境,可以直接安装,如果没有,需要先行安装虚拟机软 件。 Linux的安装步骤: (1)选择界面安装 (2)语言选择 (3)选择安装信息 (4)选择安装软件 (5)设置磁盘分区方案 (6)手动分区 (7)设置根口令及验证 2、基本 shell命令 1)pwd命令:显示当前所在目录 (2)date命令:显示日期 ()who命令:显示当前正在系统中的所有用户名字,使用终端设备号, 注册时间 ()cal命令:显示某年内指定的日历 (5) uname命令:查看当前操作系统的信息 (6)wc命令:统计文件中的文件行数、字数和字符数 (7) clear命令:清屏 3、在线帮助命令 ()man命令名:显示该命令的功能、选项等说明() whatis命令名:只给出简单介绍信息 (3)nfo命令名:另外一种帮助命令 (4)help命令名:显示内置命令( shell) 4、 Shell命令的高级操作 (1) shell的命令补全: 先输入命令的前几个字母,然后按[TAB]键,如果与输入字母匹配的 仅有一个命令名或文件名,系统将自动补全,如果有多个与之匹配,系统 将发出报警声音,如果再按一下[TAB]键,系统将列出所有与之匹配的命 令或文件名。 (2) shel的历史命令: 在提示符下输入 history命令查看所有历史命令若执行以前历史命令 列表中的某一个命令,则执行“!n”,n为历史命令列表中的编号。向上 方向键可以翻出最近使用的命令 (3) shell的重定向 输出重定向符号有“>”和“>>”,输入重定向符为“<”。错误重定向 输出“2>”,把命令行出错的信息保存到指定的文件中去 4)shel1的管道操作: 管道线“”前面的命令的输出是管道线“”后面命令的输入。 命令1命令2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值