oslab1 进程、线程相关编程实验
操作系统实验lab1
计算机2305 徐国超
1-1进程
1-1-1
代码:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid, pid1;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d ",pid); /* A */
printf("child: pid1 = %d ",pid1); /* B */
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d ",pid); /* C */
printf("parent: pid1 = %d ",pid1); /* D */
wait(NULL);
}
return 0;
}
运行结果:

在子进程中 pid=0,执行A B两行代码,输出0和子进程的实际pid
在父进程中 pid=子进程的pid,执行C D两行代码,输出子进程pid和父进程实际pid
1-1-2
去掉wait()后,父进程与子进程执行顺序不确定,多次执行输出如下:

wait()会使父进程执行到该函数时进入阻塞态,子进程执行完毕后再继续执行。
1-1-3
代码:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int global=114514;
int main()
{
pid_t pid, pid1;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d ",pid); /* A */
printf("child: pid1 = %d ",pid1); /* B */
global=1919810;
printf("child: global = %d\n", global);
system("echo 'system PID:' $$");
char *args[] = {"sh", "-c", "echo 'execvp PID:' $$", NULL};
execvp("sh", args);
printf("gogogo");
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d ",pid); /* C */
printf("parent: pid1 = %d ",pid1); /* D */
global=0;
printf("parent: global = %d\n", global);
wait(NULL);
}
global=111111;
printf("befor return: global = %d\n", global);
return 0;
}
执行结果:

子进程父进程间全局变量不共享,子进程中调用system()创建子进程执行,pid改变,并继续执行后续代码,调用exec()则是直接替换进程,pid不变,后续代码不继续执行。
1-2线程
1-2-1
代码:
#include <stdio.h>
#include <pthread.h>
int share=0;
void* thread1_function(void* arg) {
for (int i = 0; i < 100000; i++) {
share += 100;
}
printf("线程1完成\n");
return NULL;
}
void* thread2_function(void* arg) {
for (int i = 0; i < 100000; i++) {
share -= 100;
}
printf("线程2完成\n");
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, thread1_function, NULL);
pthread_create(&thread2, NULL, thread2_function, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("share的值为%d\n", share);
return 0;
}
执行结果:

因为share+=100 在寄存器层次上需要多个步骤,例如需要将share加载到寄存器 然后两个线程各对各自寄存器加100,值为100,然后share的值=寄存器,实际只加了100,少了一次
1-2-2
代码:
#include <stdio.h>
#include <pthread.h>
#include<semaphore.h>
int share=0;
sem_t semaphore;
void* thread1_function(void* arg) {
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore);
share += 100;
sem_post(&semaphore);
}
printf("线程1完成\n");
return NULL;
}
void* thread2_function(void* arg) {
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore);
share -= 100;
sem_post(&semaphore);
}
printf("线程2完成\n");
return NULL;
}
int main() {
pthread_t thread1, thread2;
sem_init(&semaphore, 0, 1);
pthread_create(&thread1, NULL, thread1_function, NULL);
pthread_create(&thread2, NULL, thread2_function, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("share的值为%d\n", share);
sem_destroy(&semaphore);
return 0;
}
执行结果:

1-2-3s
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/syscall.h>
int share=0;
sem_t semaphore;
void* thread1_function(void* arg) {
printf("线程1 tid = %d,pid = %d\n", syscall(SYS_gettid), getpid());
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore);
share += 100;
sem_post(&semaphore);
}
int ret = system("echo 'system call PID:' $$");
printf("线程1 systemcall return\n");
printf("线程1完成\n");
return NULL;
}
void* thread2_function(void* arg) {
printf("线程2 tid = %d,pid = %d\n", syscall(SYS_gettid), getpid());
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore);
share -= 100;
sem_post(&semaphore);
}
int ret = system("echo 'system call PID:' $$");
printf("线程2 systemcall return\n");
printf("线程2完成\n");
return NULL;
}
int main() {
pthread_t thread1, thread2;
sem_init(&semaphore, 0, 1);
pthread_create(&thread1, NULL, thread1_function, NULL);
pthread_create(&thread2, NULL, thread2_function, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("share的值为%d\n", share);
sem_destroy(&semaphore);
return 0;
}
执行结果:

system()创建了子进程。
1-2-3e
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/syscall.h>
int share=0;
sem_t semaphore;
void* thread1_function(void* arg) {
printf("线程1 tid = %d,pid = %d\n", syscall(SYS_gettid), getpid());
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore);
share += 100;
sem_post(&semaphore);
}
char *args[] = {"sh", "-c", "echo 'system call PID:' $$", NULL};
execvp("sh", args);
printf("线程1 systemcall return\n");
printf("线程1完成\n");
return NULL;
}
void* thread2_function(void* arg) {
printf("线程2 tid = %d,pid = %d\n", syscall(SYS_gettid), getpid());
for (int i = 0; i < 100000; i++) {
sem_wait(&semaphore);
share -= 100;
sem_post(&semaphore);
}
char *args[] = {"sh", "-c", "echo 'system call PID:' $$", NULL};
execvp("sh", args);
printf("线程2 systemcall return\n");
printf("线程2完成\n");
return NULL;
}
int main() {
pthread_t thread1, thread2;
sem_init(&semaphore, 0, 1);
pthread_create(&thread1, NULL, thread1_function, NULL);
pthread_create(&thread2, NULL, thread2_function, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("share的值为%d\n", share);
sem_destroy(&semaphore);
return 0;
}
执行结果:

exec()只是替换,没有改变pid。
1-3自旋锁
代码:
#include <stdio.h>
#include <pthread.h>
// 定义自旋锁结构体
typedef struct {
int flag;
} spinlock_t;
// 初始化自旋锁
void spinlock_init(spinlock_t *lock) {
lock->flag = 0;
}
// 获取自旋锁
void spinlock_lock(spinlock_t *lock) {
while (__sync_lock_test_and_set(&lock->flag, 1)) {
// 自旋等待
}
}
// 释放自旋锁
void spinlock_unlock(spinlock_t *lock) {
__sync_lock_release(&lock->flag);
}
// 共享变量
int shared_value = 0;
// 线程函数
void *thread_function(void *arg) {
spinlock_t *lock = (spinlock_t *)arg;
for (int i = 0; i < 5000; ++i) {
spinlock_lock(lock);
shared_value++;
spinlock_unlock(lock);
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
spinlock_t lock;
// 输出共享变量的值
printf("初始时shared_value: %d\n", shared_value);
// 初始化自旋锁
spinlock_init(&lock);
// 创建两个线程
pthread_create(&thread1, NULL, thread_function, (void *)&lock);
pthread_create(&thread2, NULL, thread_function, (void *)&lock);
// 等待线程结束
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// 输出共享变量的值
printf("最终时shared_value: %d\n", shared_value);
return 0;
}
执行结果:

自旋锁可以使某个值在被某个线程修改时让另一个线程等待,等修改完成后设置为空闲,然后另一个线程才可以修改,避免了两个线程同时修改某个值。
如果没有自旋锁?
#include <stdio.h>
#include <pthread.h>
int shared_value = 0;
void *thread_function(void *arg) {
for (int i = 0; i < 5000; ++i) {
shared_value++;
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
// 输出共享变量的值
printf("初始时shared_value: %d\n", shared_value);
// 创建两个线程
pthread_create(&thread1, NULL, thread_function, NULL);
pthread_create(&thread2, NULL, thread_function, NULL);
// 等待线程结束
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// 输出共享变量的值
printf("最终时shared_value: %d\n", shared_value);
return 0;
}
