Skip to content

Testdrace#1

Open
Mazen050 wants to merge 6 commits into
masterfrom
testdrace
Open

Testdrace#1
Mazen050 wants to merge 6 commits into
masterfrom
testdrace

Conversation

@Mazen050

Copy link
Copy Markdown
Owner

No description provided.

@Mazen050

Mazen050 commented Feb 28, 2026

Copy link
Copy Markdown
Owner Author

#include "kernel/types.h"
#include "user/user.h"

int
main(void)
{
int fd[2];
char buf[50];

// Create pipe
if (pipe(fd) < 0) {
    fprintf(2, "pipe failed\n");
    exit(1);
}

int pid = fork();

if (pid < 0) {
    fprintf(2, "fork failed\n");
    exit(1);
}

if (pid == 0) {
    // CHILD PROCESS → Reader
    close(fd[1]);   // close write end

    read(fd[0], buf, sizeof(buf));
    printf("Child received: %s\n", buf);

    close(fd[0]);
} else {
    // PARENT PROCESS → Writer
    close(fd[0]);   // close read end

    char msg[] = "Hello from parent!";
    write(fd[1], msg, sizeof(msg));

    close(fd[1]);
    wait(0);
}

exit(0);

}

@Mazen050

Copy link
Copy Markdown
Owner Author

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t pid = fork();

if (pid < 0) {
    // Fork failed
    perror("Fork failed");
    exit(1);
}

else if (pid == 0) {
    // Child process
    printf("Child process: Executing ls...\n");

    execlp("ls", "ls", NULL);

    // If exec fails
    perror("Exec failed");
    exit(1);
}

else {
    // Parent process
    wait(NULL);
    printf("Parent process: Child finished execution.\n");
}

exit(0);

}

@Mazen050

Copy link
Copy Markdown
Owner Author

#include <omp.h>
#include <stdio.h>

int main() {
    int N = 100;
    int sum = 0;
    int partial_sum[4] = {0}; // assuming max 4 threads

    int nthreads = omp_get_num_threads(); // Number of thread outside parallel
    printf("Number of available threads: %d\n", nthreads);

    int num_threads = 4;
    omp_set_num_threads(num_threads);

    #pragma omp parallel
    {
        int nthreads = omp_get_num_threads(); // Number of thread inside parallel
        #pragma omp single
        {
            printf("Number of available threads: %d\n", nthreads);
        }

        int tid = omp_get_thread_num();
        printf("I'm in thread: %d\n", tid);

        #pragma omp for // Divides loop iterations between the spawned threads
        for (int i = 1; i <= N; i++) {
            partial_sum[tid] += i;
        }
    }

    // TODO: Combine partial sums
    for (int i = 0; i < num_threads; i++) {
        sum += partial_sum[i];
    }

    printf("Final sum from 1 to %d is: %d\n", N, sum);
    printf("Expected sum: %d\n", N * (N + 1) / 2);

    return 0;
}

@Mazen050

Copy link
Copy Markdown
Owner Author
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *worker(void *arg) {
    char *message = (char *)arg;
    printf("Thread says: %s\n", message);
    return NULL;
}

int main() {
    pthread_t thread1, thread2;

    char *msg1 = "Hello from Thread 1!";
    char *msg2 = "Hello from Thread 2!";

    // Create threads
    pthread_create(&thread1, NULL, worker, msg1);
    pthread_create(&thread2, NULL, worker, msg2);

    // Wait for threads to finish
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    printf("Main thread finished.\n");
    return 0;
}

@Mazen050

Mazen050 commented Apr 27, 2026

Copy link
Copy Markdown
Owner Author
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS 5
#define INCREMENTS_PER_THREAD 100000

int counter = 0; // Shared resource

void *increment_counter(void *arg) {
    for (int i = 0; i < INCREMENTS_PER_THREAD; i++) {
        counter++;
    }
    return NULL;
}

int main() {
    pthread_t threads[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_create(&threads[i], NULL, increment_counter, NULL);
    }

    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_join(threads[i], NULL);
    }

    printf("Final counter value: %d\n", counter);
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant