Last active
February 20, 2018 23:21
-
-
Save rebeccajae/e4d229ae13af482119d75e1ea4b8b010 to your computer and use it in GitHub Desktop.
ECE 426 Lab 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| int main() | |
| { | |
| int n = 8; //35 | |
| pid_t pid; | |
| pid = fork(); | |
| if (pid == 0) { /* child process */ | |
| while(n > 1){ | |
| printf("%d - ", n); | |
| if(n % 2 == 0){ | |
| //even | |
| n = n/2; | |
| }else{ | |
| n = 3*n + 1; | |
| } | |
| } | |
| printf("%d\n", n); | |
| return 0; | |
| } | |
| else if (pid > 0) { /* parent process */ | |
| wait(NULL); | |
| printf ("Child Exited\n"); | |
| return 0; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| int main(int argc, char* argv[]) | |
| { | |
| if(argc == 1){ | |
| printf("Usage: ./lab3b <Number>\n"); | |
| return -1; | |
| } | |
| int n = atoi(argv[1]); | |
| pid_t pid; | |
| pid = fork(); | |
| if (pid == 0) { /* child process */ | |
| while(n > 1){ | |
| printf("%d - ", n); | |
| if(n % 2 == 0){ | |
| //even | |
| n = n/2; | |
| }else{ | |
| n = 3*n + 1; | |
| } | |
| } | |
| printf("%d\n", n); | |
| return 0; | |
| } | |
| else if (pid > 0) { /* parent process */ | |
| wait(NULL); | |
| printf ("Child Exited\n"); | |
| return 0; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <fcntl.h> | |
| #include <sys/shm.h> | |
| #include <sys/stat.h> | |
| #include <sys/mman.h> | |
| int main(int argc, char* argv[]) | |
| { | |
| if(argc == 1){ | |
| printf("Usage: ./lab3c-prod <Number>\n"); | |
| return -1; | |
| } | |
| const int SIZE = 4096; | |
| const char *name = "OS"; | |
| int n = atoi(argv[1]); | |
| int shm_fd; | |
| void *ptr; | |
| shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); | |
| ftruncate(shm_fd,SIZE); | |
| ptr = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); | |
| if (ptr == MAP_FAILED) { | |
| printf("Map failed\n"); | |
| return -1; | |
| } | |
| while(n > 1){ | |
| sprintf(ptr, "%d - ", n); | |
| ptr += snprintf(NULL, 0, "%d - ", n); | |
| if(n % 2 == 0){ | |
| //even | |
| n = n/2; | |
| }else{ | |
| n = 3*n + 1; | |
| } | |
| } | |
| sprintf(ptr,"%d\n", n); | |
| ptr += snprintf(NULL, 0, "%d\n", n); | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The generic consumer program from the book can consume lab3c.