Skip to content

Instantly share code, notes, and snippets.

@avegancafe
Last active November 13, 2015 20:50
Show Gist options
  • Select an option

  • Save avegancafe/fb6c4f6ce62667ba3280 to your computer and use it in GitHub Desktop.

Select an option

Save avegancafe/fb6c4f6ce62667ba3280 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void ClientProcess(int []);
void main(int argc, char *argv[])
{
int ShmID;
int *ShmPTR;
pid_t pid;
int status;
if (argc != 5) {
printf("Use: %s #1 #2 #3 #4\n", argv[0]);
exit(1);
}
ShmID = shmget(IPC_PRIVATE, 4*sizeof(int), IPC_CREAT | 0666);
if (ShmID < 0) {
printf("*** shmget error (server) ***\n");
exit(1);
}
printf("Server has received a shared memory of four integers...\n");
ShmPTR = (int *) shmat(ShmID, NULL, 0);
if ((int) ShmPTR == -1) {
printf("*** shmat error (server) ***\n");
exit(1);
}
printf("Server has attached the shared memory...\n");
ShmPTR[0] = atoi(argv[1]);
ShmPTR[1] = atoi(argv[2]);
ShmPTR[2] = atoi(argv[3]);
ShmPTR[3] = atoi(argv[4]);
printf("Server has filled %d %d %d %d in shared memory...\n",
ShmPTR[0], ShmPTR[1], ShmPTR[2], ShmPTR[3]);
printf("Server is about to fork a child process...\n");
pid = fork();
if (pid < 0) {
printf("*** fork error (server) ***\n");
exit(1);
}
else if (pid == 0) {
ClientProcess(ShmPTR);
exit(0);
}
wait(&status);
printf("Server has detected the completion of its child...\n");
shmdt((void *) ShmPTR);
printf("Server has detached its shared memory...\n");
shmctl(ShmID, IPC_RMID, NULL);
printf("Server has removed its shared memory...\n");
printf("Server exits...\n");
exit(0);
}
void ClientProcess(int SharedMem[])
{
printf(" Client process started\n");
printf(" Client found %d %d %d %d in shared memory\n",
SharedMem[0], SharedMem[1], SharedMem[2], SharedMem[3]);
printf(" Client is about to exit\n");
}
0 2 1 -1
2 0 4 3
1 4 0 2
-1 3 2 0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <setjmp.h>
#include <ctype.h>
#include <unistd.h>
static int** perms;
static int strInd = 0;
#define MAX_NODES 100
#define MAX_DISTANCE 255
static int mat[MAX_NODES][MAX_NODES];
static int numNodes;
#define MAX_THREADS 100
#define STACK_SIZE 16384
static jmp_buf finish; // Defines goto location after last thread finishes.
/* Machine context data structures and variables. */
typedef struct mctx_t {
jmp_buf jb;
} mctx_t;
static mctx_t jmps[MAX_THREADS];
static char * stackPointers[MAX_THREADS];
static struct mctx_t mctx_caller;
static sig_atomic_t mctx_called;
static struct mctx_t *mctx_creat;
static void (*mctx_creat_func)(void *);
static void *mctx_creat_arg;
static sigset_t mctx_creat_sigs;
int max_fucking_threads;
/* Save machine context. */
#define mctx_save(mctx) setjmp((mctx)->jb)
/* Restore machine context. */
#define mctx_restore(mctx) longjmp((mctx)->jb, 1)
/* Switch machine context. */
#define mctx_switch(mctx_old, mctx_new) if (setjmp((mctx_old)->jb) == 0) longjmp((mctx_new)->jb,1)
void mctx_create_boot(void) {
void (*mctx_start_func)(void *);
void *mctx_start_arg;
/* Step 10: */
sigprocmask(SIG_SETMASK, &mctx_creat_sigs, NULL);
/* Step 11: */
mctx_start_func = mctx_creat_func;
mctx_start_arg = mctx_creat_arg;
/* Step 12 and Step 13: */
mctx_switch(mctx_creat, &mctx_caller);
/* The thread "magically" starts... */
mctx_start_func(mctx_start_arg);
/* NOTREACHED */
abort();
}
void mctx_create_trampoline(int sig) {
/* Step 5: */
if (mctx_save(mctx_creat) == 0) {
mctx_called = 1;
return;
}
/* Step 9: */
mctx_create_boot();
}
void mctx_create(mctx_t *mctx, void (*sf_addr)(void *), void *sf_arg, void *sk_addr, size_t sk_size) {
struct sigaction sa;
struct sigaction osa;
struct sigaltstack ss;
struct sigaltstack oss;
sigset_t osigs;
sigset_t sigs;
/* Step 1: */
sigemptyset(&sigs);
sigaddset(&sigs, SIGUSR1);
sigprocmask(SIG_BLOCK, &sigs, &osigs);
/* Step 2: */
memset((void *) &sa, 0, sizeof (struct sigaction));
sa.sa_handler = mctx_create_trampoline;
sa.sa_flags = SA_ONSTACK;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, &osa);
/* Step 3: */
ss.ss_sp = sk_addr;
ss.ss_size = sk_size;
ss.ss_flags = 0;
sigaltstack(&ss, &oss);
/* Step 4: */
mctx_creat = mctx;
mctx_creat_func = sf_addr;
mctx_creat_arg = sf_arg;
mctx_creat_sigs = osigs;
mctx_called = 0;
kill(getpid(), SIGUSR1);
sigfillset(&sigs);
sigdelset(&sigs, SIGUSR1);
while (!mctx_called) {
sigsuspend(&sigs);
}
/* Step 6: */
sigaltstack(NULL, &ss);
ss.ss_flags = SS_DISABLE;
sigaltstack(&ss, NULL);
if (!(oss.ss_flags & SS_DISABLE))
sigaltstack(&oss, NULL);
sigaction(SIGUSR1, &osa, NULL);
sigprocmask(SIG_SETMASK, &osigs, NULL);
/* Step 7 and Step 8: */
mctx_switch(&mctx_caller, mctx);
/* Step 14: */
return;
}
int verifyEdges(int path[], int mat[MAX_NODES][MAX_NODES]) {
int i;
int prev = path[0];
for (i = 1; i < numNodes+1; i++) {
if (mat[prev][path[i]] == -1) return 0;
prev = path[i];
}
return 1;
}
int* prefixint(int* src, int len, int notThis) {
int* fin = malloc( (len + 2) * sizeof(int) );
fin[0] = notThis;
memcpy(fin+1, src, len * sizeof(int));
fin[len-1] = notThis;
return fin;
}
int* intdup(int* src, int len) {
int* fin = malloc( len * sizeof(int) );
memcpy(fin, src, len * sizeof(int));
return fin;
}
void swap(char *x, char *y) {
char temp;
temp = *x;
*x = *y;
*y = temp;
} // endof [swap]
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
}
void permute(int* a, int l, int r, int notThis) {
int i;
int j;
if (l == r) {
a = prefixint(a, numNodes+1, notThis);
if (verifyEdges(a, mat)){
perms[strInd++] = intdup(a, numNodes+1);
}
} else {
for (i = l; i <= r; i++) {
int tmp = a[l];
a[l] = a[i];
a[i] = tmp;
permute(a, l+1, r, notThis);
tmp = a[l]; // backtrack
a[l] = a[i];
a[i] = tmp;
}
} // endof [if (l == r)]
} // endof [permute]
void createAwesomeThread(void (*func) (int), int id) {
int stackSize = STACK_SIZE / 2;
stackPointers[id] = (char *) malloc(STACK_SIZE);
mctx_create(&jmps[id], (void (*) (void *)) func,
(void *) (long)id, stackPointers[id] + stackSize, stackSize);
}
// void stupid_shit(int x) {
// int penisTime = rand() % 5;
// printf("lol testing: %d\n", x);
// //sleep(penisTime);
// if (x != max_fucking_threads) {
// mctx_switch(&jmps[x], &jmps[x + 1]);
// }
// longjmp(finish, 0);
// }
char* itoa(int val, int base){
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return buf[i+1] != 0 ? &buf[i+1] : "0\0";
} // endof [itoa]
void findPaths (int notThis) { // to is inclusive
int to = max_fucking_threads;
int i;
int j;
int buf[to];
for (i = 0, j=0; i <= to; i++) {
if (i != notThis) {
buf[j++] = i;
}
}
permute(buf, 0, to-1, notThis);
if (notThis != max_fucking_threads) {
mctx_switch(&jmps[notThis], &jmps[notThis + 1]);
}
longjmp(finish, 0);
//return; // DO I NEED THIS?
}
int main(int argc, char* argv[]) {
perms = calloc(MAX_NODES, MAX_NODES * (3 + 1));
char* token;
int i;
int j;
char line[ MAX_NODES * (3 + 1) ];
fgets(line, MAX_NODES * (3 + 1), stdin);
token = strtok(line, " ");
numNodes = 0;
while(token != NULL) {
mat[0][numNodes] = atoi(token);
token = strtok(NULL, " ");
numNodes++;
}
for (i = 1; i < numNodes; i++) {
j = 0;
fgets(line, MAX_NODES * (3 + 1), stdin);
token = strtok(line, " ");
while(token != NULL) {
mat[i][j] = atoi(token);
token = strtok(NULL, " ");
j++;
} // endof while
} // endof [for]
int n = numNodes;
max_fucking_threads = n-1;
for (i = 0; i < n; i++) {
printf("Thread %d created\n", i);
createAwesomeThread(&findPaths, i);
}
printf("DONE BITCH\n");
printf("threads created?\n");
if (setjmp(finish) == 0) {
mctx_switch(&mctx_caller, &jmps[0]);
}
printf("oh fuck\n");
for (i = 0; i < n; i++) {
free(stackPointers[i]);
}
for (i = 0; (i < factorial(numNodes-1)*numNodes) && (perms[i] != NULL); i++) {
for (j = 0; j < numNodes + 1; j++) {
printf("%d", perms[i][j]);
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment