0% found this document useful (0 votes)
19 views

atividade2SO 2

This C code defines a program that uses two child processes to write numbers and letters to separate text files. It forks the first child process to write numbers 1-10 to "numbers.txt". It forks the second child process to write letters A-Z to "letters.txt". It then waits for the child processes to finish, closes the file descriptors, and prints the contents of both output files.

Uploaded by

marianaixborges
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

atividade2SO 2

This C code defines a program that uses two child processes to write numbers and letters to separate text files. It forks the first child process to write numbers 1-10 to "numbers.txt". It forks the second child process to write letters A-Z to "letters.txt". It then waits for the child processes to finish, closes the file descriptors, and prints the contents of both output files.

Uploaded by

marianaixborges
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

04-

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>

int main() {

pid_t child_pid1, child_pid2;

int status;

int fd_numbers = open("numbers.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);

if (fd_numbers == -1) {

perror("Erro ao criar o arquivo numbers.txt");

exit(1);

int fd_letters = open("letters.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);

if (fd_letters == -1) {

perror("Erro ao criar o arquivo letters.txt");

exit(1);

child_pid1 = fork();

if (child_pid1 == 0) {

for (int i = 1; i <= 10; i++) {

dprintf(fd_numbers, "%d ", i);

exit(0);

} else if (child_pid1 < 0) {

perror("Erro na criação do processo filho 1");

exit(1);

}
child_pid2 = fork();

if (child_pid2 == 0) {

for (char letter = 'A'; letter <= 'Z'; letter++) {

dprintf(fd_letters, "%c ", letter);

exit(0);

} else if (child_pid2 < 0) {

perror("Erro na criação do processo filho 2");

exit(1);

waitpid(child_pid1, &status, 0);

waitpid(child_pid2, &status, 0);

close(fd_numbers);

close(fd_letters);

printf("Conteúdo do arquivo numbers.txt:\n");

int ch;

fd_numbers = open("numbers.txt", O_RDONLY);

while ((ch = getchar()) != EOF) {

putchar(ch);

close(fd_numbers);

printf("\nConteúdo do arquivo letters.txt:\n");

fd_letters = open("letters.txt", O_RDONLY);

while ((ch = getchar()) != EOF) {

putchar(ch);

close(fd_letters);
return 0;

05 –

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>

int main() {

char command[100];

while (1) {

printf("Digite um comando (ou 'exit' para sair): ");

fgets(command, sizeof(command), stdin);

command[strcspn(command, "\n")] = '\0';

if (strcmp(command, "exit") == 0) {

printf("Saindo do programa.\n");

break;

pid_t child_pid = fork();

if (child_pid < 0) {

perror("Falha na criação do processo filho");

exit(1);

} else if (child_pid == 0) {

execlp(command, command, (char *) NULL); // Executa o comando


perror("Erro na execução do comando");

exit(1);

} else {

int status;

wait(&status); // Aguarda o término do processo filho

if (WIFEXITED(status)) {

printf("O comando retornou com status %d\n", WEXITSTATUS(status));

return 0;

You might also like