Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed lab3a_p1 to have the correct code.

  • Loading branch information...
commit 44b8df6cc06069fbcc4f77f9d64479fc2acf53cd 1 parent 52c7e7a
@jpvi authored
Showing with 106 additions and 137 deletions.
  1. +106 −44 03a/lab3a_p1.c
  2. +0 −93 03a/lab3a_p1_win.c
View
150 03a/lab3a_p1.c
@@ -1,69 +1,131 @@
#include <stdio.h>
#include <stdlib.h>
-#include <pthread.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
#include <sys/types.h>
-void add_to_global();
+void fork_without_sleep();
+void fork_with_sleep();
-/* Global variable to be added to using threads. */
-int global = 0;
+void catcher(int);
+void forking(int);
-/* Global mutex lock. */
-pthread_mutex_t lock;
+int main(int argc, char** argv) {
+ fork_without_sleep();
+ fork_with_sleep();
-int main() {
- int i;
- int return_value;
- pthread_t tid[10];
- void *arg;
-
- /* printf("Create threads without sleep().\n"); */
- printf("Create threads with sleep().\n");
+ return 0;
+}
- return_value = pthread_mutex_init(&lock, NULL);
+void catcher(int sig) {
+ printf("Signal catcher called for signal %d.\n", sig);
+}
- if(return_value < 0) {
- perror("Can't initialize mutex");
- exit(-1);
+void fork_without_sleep() {
+ int i;
+ int proc;
+ int result = 0;
+ int signal;
+ int sig_pid;
+ pid_t child_pid;
+ sigset_t waitset;
+ struct sigaction sigact;
+
+ sigemptyset(&sigact.sa_mask);
+ sigact.sa_flags = 0;
+ sigact.sa_handler = catcher;
+ sigaction(SIGUSR1, &sigact, NULL);
+ sigemptyset(&waitset);
+ sigaddset(&waitset, SIGUSR1);
+ sigprocmask(SIG_BLOCK, &waitset, NULL);
+
+ sig_pid = getpid();
+
+ printf("Part 1 without sleep():\n");
+
+ for(proc = 0; proc < 10; proc++) {
+ child_pid = fork();
+
+ if(child_pid) {
+ break;
+ }
}
- for(i = 1; i <= 10; i++) {
- return_value = pthread_create(&tid[i], NULL, (void *)add_to_global, arg);
+ if(sig_pid != getpid()) {
+ result = sigwait(&waitset, &signal);
- if(return_value < 0) {
- perror("Cannot create thread.");
- exit(-1);
+ if(result == 0) {
+ printf("PID: %d, sigwait() returned for signal %d.\n", getpid(), signal);
+ }
+ else {
+ printf("sigwait() returned error number %d.\n", errno);
+ perror("sigwait() function failed.\n");
}
}
- /* Join threads together. */
- for(i = 1; i <= 10; i++) {
- pthread_join(tid[i], NULL);
+ for(i = 0; i < 10; i++ ) {
+ fprintf(stderr, "Process: %d, PID: %d\n", i+1, getpid());
}
- return 0;
+ if(kill(child_pid, SIGUSR1) < 0) {
+ perror("Failed to kill child");
+ }
+
+ wait(child_pid);
}
-void add_to_global(void* arg) {
- /* Enter critical section. */
- pthread_mutex_lock(&lock);
+void fork_with_sleep() {
+ int i;
+ int proc;
+ int result = 0;
+ int signal;
+ int sig_pid;
+ pid_t child_pid;
+ sigset_t waitset;
+ struct sigaction sigact;
+
+ sigemptyset(&sigact.sa_mask);
+ sigact.sa_flags = 0;
+ sigact.sa_handler = catcher;
+ sigaction(SIGUSR1, &sigact, NULL);
+ sigemptyset(&waitset);
+ sigaddset(&waitset, SIGUSR1);
+ sigprocmask(SIG_BLOCK, &waitset, NULL);
+
+ sig_pid = getpid();
+
+ printf("Part 1 with sleep():\n");
+
+ for(proc = 0; proc < 10; proc++) {
+ child_pid = fork();
+
+ if(child_pid) {
+ break;
+ }
- int local;
+ sleep(1);
+ }
- fprintf(stderr, "Hello, I'm thread %u.\n", (unsigned int)pthread_self());
- local = global;
- sleep(1);
+ if(sig_pid != getpid()) {
+ result = sigwait(&waitset, &signal);
- fprintf(stderr, "Local: %d, TID: %u\n", local, (unsigned int)pthread_self());
- local += 10;
- sleep(1);
+ if(result == 0) {
+ printf("PID: %d, sigwait() returned for signal %d.\n", getpid(), signal);
+ }
+ else {
+ printf("sigwait() returned error number %d.\n", errno);
+ perror("sigwait() function failed.\n");
+ }
+ }
- fprintf(stderr, "Local: %d, TID: %u\n", local, (unsigned int)pthread_self());
- global = local;
+ for(i = 0; i < 10; i++ ) {
+ fprintf(stderr, "Process: %d, PID: %d\n", i+1, getpid());
+ }
- /* Exit critical section. Comment this line out to create deadlock. */
- /* pthread_mutex_unlock(&lock); */
+ if(kill(child_pid, SIGUSR1) < 0) {
+ perror("Failed to kill child");
+ }
- /* Remainder section. */
- sleep(1);
-}
+ wait(child_pid);
+}
View
93 03a/lab3a_p1_win.c
@@ -1,93 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <windows.h>
-
-void CreateProcessWithSleep();
-void CreateProcessWithoutSleep();
-void CreateProcessUsingOthers();
-
-int main() {
- /* Comment out or uncomment each function and run them one at a time. */
- /*
- CreateProcessWithoutSleep();
- CreateProcessWithSleep();
- CreateProcessUsingOthers();
- */
-
- return 0;
-}
-
-void CreateProcessWithoutSleep() {
- int i;
- STARTUPINFO si[10];
- PROCESS_INFORMATION pi[10];
-
- printf("Part 1 without Sleep():\n");
-
- for(i = 0; i < 10; i++) {
- ZeroMemory(&si[i], sizeof(si[i]));
- si[i].cb = sizeof(si[i]);
- ZeroMemory(&pi[i], sizeof(pi[i]));
- GetStartupInfo(&si[i]);
-
- if(!CreateProcess(NULL, "proc.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si[i], &pi[i])) {
- fprintf(stderr, "Create Process Failed\n");
- exit(-1);
- }
- }
-
- for(i = 0; i < 10; i++) {
- WaitForSingleObject(pi[i].hProcess, INFINITE);
- }
-}
-
-void CreateProcessWithSleep() {
- int i;
- STARTUPINFO si[10];
- PROCESS_INFORMATION pi[10];
-
- printf("Part 1 with Sleep():\n");
-
- for(i = 0; i < 10; i++) {
- ZeroMemory(&si[i], sizeof(si[i]));
- si[i].cb = sizeof(si[i]);
- ZeroMemory(&pi[i], sizeof(pi[i]));
- GetStartupInfo(&si[i]);
-
- if(!CreateProcess(NULL, "proc.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si[i], &pi[i])) {
- fprintf(stderr, "Create Process Failed\n");
- exit(-1);
- }
-
- Sleep(1);
- }
-
- for(i = 0; i < 10; i++) {
- WaitForSingleObject(pi[i].hProcess, INFINITE);
- }
-}
-
-void CreateProcessUsingOthers() {
- int i;
- STARTUPINFO si[10];
- PROCESS_INFORMATION pi[10];
-
- printf("Part 1 using one fprintf()/fputs()/WriteFile():\n");
-
- for(i = 0; i < 10; i++) {
- ZeroMemory(&si[i], sizeof(si[i]));
- si[i].cb = sizeof(si[i]);
- ZeroMemory(&pi[i], sizeof(pi[i]));
- GetStartupInfo(&si[i]);
-
- if(!CreateProcess(NULL, "proc2.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si[i], &pi[i])) {
- fprintf(stderr, "Create Process Failed\n");
- exit(-1);
- }
- }
-
- for(i = 0; i < 10; i++) {
- WaitForSingleObject(pi[i].hProcess, INFINITE);
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.