init commit of lab3_challenge2

lab3_challenge2_semaphore
Zhiyuan Shao 2 years ago
parent 448d994bba
commit 06f549a9e6

@ -3,9 +3,10 @@ Copyright License
========================================================================== ==========================================================================
The PKE software is: The PKE software is:
Copyright (c) 2021, Zhiyuan Shao (zyshao@hust.edu.cn), Copyright (c) 2021, Zhiyuan Shao (zyshao@hust.edu.cn),
Yi Gui (gy163email@163.com), Ziming Yuan (1223962053@qq.com),
Yan Jiao (773709579@qq.com), Yixin Song (yixinsong@hust.edu.cn),
Boyang Li (liboyang_hust@163.com)
Huazhong University of Science and Technology Huazhong University of Science and Technology
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining

@ -70,7 +70,7 @@ USER_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(USER_CPPS)))
USER_TARGET := $(OBJ_DIR)/app_two_long_loops USER_TARGET := $(OBJ_DIR)/app_semaphore
#------------------------targets------------------------ #------------------------targets------------------------
$(OBJ_DIR): $(OBJ_DIR):
@-mkdir -p $(OBJ_DIR) @-mkdir -p $(OBJ_DIR)

@ -0,0 +1,30 @@
/*
* This app create two child process.
* Use semaphores to control the order of
* the main process and two child processes print info.
*/
#include "user/user_lib.h"
#include "util/types.h"
int main(void) {
int main_sem, child_sem[2];
main_sem = sem_new(1);
for (int i = 0; i < 2; i++) child_sem[i] = sem_new(0);
int pid = fork();
if (pid == 0) {
pid = fork();
for (int i = 0; i < 10; i++) {
sem_P(child_sem[pid == 0]);
printu("Child%d print %d\n", pid == 0, i);
if (pid != 0) sem_V(child_sem[1]); else sem_V(main_sem);
}
} else {
for (int i = 0; i < 10; i++) {
sem_P(main_sem);
printu("Parent print %d\n", i);
sem_V(child_sem[0]);
}
}
exit(0);
return 0;
}

@ -1,28 +0,0 @@
/*
* The application of lab3_3.
* parent and child processes never give up their processor during execution.
*/
#include "user/user_lib.h"
#include "util/types.h"
int main(void) {
uint64 pid = fork();
uint64 rounds = 100000000;
uint64 interval = 10000000;
uint64 a = 0;
if (pid == 0) {
printu("Child: Hello world! \n");
for (uint64 i = 0; i < rounds; ++i) {
if (i % interval == 0) printu("Child running %ld \n", i);
}
} else {
printu("Parent: Hello world! \n");
for (uint64 i = 0; i < rounds; ++i) {
if (i % interval == 0) printu("Parent running %ld \n", i);
}
}
exit(0);
return 0;
}
Loading…
Cancel
Save