所以我必须使用fork创建7个子进程,并且每个进程必须循环工作直到药物(meds)耗尽。但他们一次只运行一个,有时只运行一次。变量和其他名称都是pt-br,但我会翻译。 因此,它应该是一个疗养院(一个可怕的heheheh),它的药物数量有限。一个人需要1丸才能睡4小时,8小时需要8小时。此外,只有5张床,因此排队等候#34;用于床。一旦有了一张免费的床,一个醒着等待睡觉的老人将随机服用1或2粒药片并入睡。在那之后,他会离开床,让另一个老人睡觉。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define ACORDADO 0 //awake
#define ESPERANDO 1 //waiting
#define DORMINDO 2 //sleeping
#define N 7
void mostrar(void); //show info
void dormir(int); //sleep
void tomar_med(int); //take meds
void esperar(int);//wait
void acordar(int);//wake up
void liberar_cama(int);//leave the bed
void acao_idoso(int j);//elder action
int medicamentos = 90; //meds
int camas = 5; //beds
int idosos [N]; //elders
int i;
void forker(int n_proc)
{
pid_t pid;
if(n_proc > 0)
{
if ((pid = fork()) < 0){
perror("fork");
}else if (pid == 0){ //filho
acao_idoso(n_proc - 1);
}else if(pid > 0){ //pai
mostrar();
forker(n_proc - 1);
}
}
}
void main(){
forker(7);
}
void mostrar(){
for(i=0;i<N;i++){
if(idosos[i] == ACORDADO)
printf("O idoso %d esta acordado.\n", i+1); //elder is awake
if(idosos[i] == ESPERANDO)
printf("O idoso %d esta esperando.\n", i+1); //elder is waiting
if(idosos[i] == DORMINDO)
printf("O idoso %d esta dormindo.\n", i+1); //elder is asleep
}
printf("\n");
printf("%d medicamentos sobrando.\n", medicamentos); //%d meds left
printf("\n");
}
void acao_idoso(int j){
int i = j;
while(medicamentos != 0){
acordar(i);
mostrar();
esperar(i);
mostrar();
dormir(i);
mostrar();
liberar_cama(i);
mostrar();
}
}
void acordar(int i){
if(idosos[i] == ACORDADO){
int t = rand() %11;
sleep(t+1);
}
}
void esperar(int i){
if(idosos[i] == ACORDADO && medicamentos > 0)
idosos[i] = ESPERANDO;
}
void dormir(int i){
if((idosos[i] == ESPERANDO) && (camas >= 1)){
int j = rand();
if(i % 2 == 0){
medicamentos = medicamentos - 2;
sleep(8);
}else{
medicamentos = medicamentos - 1;
sleep(4);
}
camas = camas - 1;
idosos[i] = DORMINDO;
}
}
void liberar_cama(int i){
if(idosos[i] == DORMINDO){
idosos[i] = ACORDADO;
camas++;
}
}
答案 0 :(得分:0)
如评论中所述,您的主程序需要等待孩子完成,并且您需要在j
中使用dormir()
。
如果您一次睡4,8秒,则需要很长时间(10分钟以上)才能完成。将睡眠调用更改为sleep10(int n)
以休眠n
十分之一秒,并使用nanosleep()
提供实现,代码在75秒内完成。
当你只剩一颗药丸时,你还需要确保没有人吃2粒药;得到消极的药剂sobrando&#39;让程序无限期地运行。
在Linux上,void main()
无条件错误;除非您有其他要返回的状态,否则请在int main(void)
的末尾使用return 0;
和(最好)明确的main()
。
代码还会在消息之前输出PID编号,以确定哪个进程正在执行哪些操作。有空间认为应该打印更多的信息,但我的西班牙语根本不存在,我不会尝试它。 main()
计划报告孩子退出时的退出状态。
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
#define ACORDADO 0
#define ESPERANDO 1
#define DORMINDO 2
#define N 7
void mostrar(void);
void dormir(int);
void tomar_med(int);
void esperar(int);
void acordar(int);
void liberar_cama(int);
void acao_idoso(int j);
void forker(int n_proc);
int medicamentos = 90;
int camas = 5;
int idosos[N];
int main(void)
{
forker(N);
int corpse;
int status;
while ((corpse = wait(&status)) > 0)
printf("PID %5d exited with status 0x%.4X\n", corpse, status);
return 0;
}
static void sleep10(int n)
{
assert(n > 0 && n < 100);
struct timespec ts = { .tv_sec = n / 10, .tv_nsec = (n % 10) * 100000000 };
printf("%d: sleeping %d.%ds\n", (int)getpid(), n / 10, n % 10);
nanosleep(&ts, 0);
}
void forker(int n_proc)
{
pid_t pid;
if (n_proc > 0)
{
if ((pid = fork()) < 0)
{
perror("fork");
}
else if (pid == 0)
{
printf("%d: PID %d\n", n_proc, (int)getpid());
acao_idoso(n_proc - 1);
}
else if (pid > 0)
{
mostrar();
forker(n_proc - 1);
}
}
}
void mostrar()
{
int pid = getpid();
for (int i = 0; i < N; i++)
{
if (idosos[i] == ACORDADO)
printf("%d: O idoso %d esta acordado.\n", pid, i + 1);
if (idosos[i] == ESPERANDO)
printf("%d: O idoso %d esta esperando.\n", pid, i + 1);
if (idosos[i] == DORMINDO)
printf("%d: O idoso %d esta dormindo.\n", pid, i + 1);
}
printf("\n");
printf("%d: %d medicamentos sobrando.\n", pid, medicamentos);
printf("\n");
}
void acao_idoso(int j)
{
int i = j;
while (medicamentos != 0)
{
acordar(i);
mostrar();
esperar(i);
mostrar();
dormir(i);
mostrar();
liberar_cama(i);
mostrar();
}
}
void acordar(int i)
{
if (idosos[i] == ACORDADO)
{
int t = rand() % 11;
sleep10(t + 1);
}
}
void esperar(int i)
{
if (idosos[i] == ACORDADO && medicamentos > 0)
idosos[i] = ESPERANDO;
}
static inline int min(int x, int y) { return (x < y) ? x : y; }
void dormir(int i)
{
if ((idosos[i] == ESPERANDO) && (camas >= 1))
{
int j = rand();
if (j % 2 == 0)
{
medicamentos = medicamentos - min(2, medicamentos);
sleep10(8);
}
else
{
medicamentos = medicamentos - 1;
sleep10(4);
}
camas = camas - 1;
idosos[i] = DORMINDO;
}
}
void liberar_cama(int i)
{
if (idosos[i] == DORMINDO)
{
idosos[i] = ACORDADO;
camas++;
}
}