Skip to content

Instantly share code, notes, and snippets.

@rebeccajae
Last active February 20, 2018 23:21
Show Gist options
  • Select an option

  • Save rebeccajae/e4d229ae13af482119d75e1ea4b8b010 to your computer and use it in GitHub Desktop.

Select an option

Save rebeccajae/e4d229ae13af482119d75e1ea4b8b010 to your computer and use it in GitHub Desktop.
ECE 426 Lab 3
#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;
}
}
#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;
}
}
#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;
}
@rebeccajae

Copy link
Copy Markdown
Author

The generic consumer program from the book can consume lab3c.

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