forked from Bananymous/banan-os
Kernel: Add bareboness fork() function
This commit is contained in:
@@ -1,31 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define ERROR(msg) { perror(msg); return 1; }
|
||||
#define BUF_SIZE 1024
|
||||
|
||||
int main()
|
||||
{
|
||||
FILE* fp = fopen("/usr/include/stdio.h", "r");
|
||||
if (fp == NULL)
|
||||
ERROR("fopen");
|
||||
printf("forking\n");
|
||||
|
||||
char* buffer = (char*)malloc(BUF_SIZE);
|
||||
if (buffer == NULL)
|
||||
ERROR("malloc");
|
||||
|
||||
for (;;)
|
||||
pid_t pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
size_t n_read = fread(buffer, 1, BUF_SIZE - 1, fp);
|
||||
if (n_read == 0)
|
||||
break;
|
||||
buffer[n_read] = '\0';
|
||||
printf("%s", buffer);
|
||||
printf("child\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
fclose(fp);
|
||||
printf("parent\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user