Skip to content

Instantly share code, notes, and snippets.

View dulimarta's full-sized avatar

Hans Dulimarta dulimarta

  • School of Computing and Information Systems, Grand Valley State University
  • Grand Rapids, Michigan
View GitHub Profile
@dulimarta
dulimarta / posix-shm.c
Last active October 20, 2020 19:33
CS452 Lab06 - POSIX shm
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <time.h>
// New type definitions
@dulimarta
dulimarta / switch_2threads.c
Created September 25, 2020 14:10
CS452 ULT swap_conteext
/*
* Demonstrate a minimal code to implement thread switching in user
* space
**/
#include <ucontext.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
@dulimarta
dulimarta / lab03_c.c
Last active February 13, 2023 02:09
CS452 Lab03 - fork() and signal()
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
bool running = true;
void sigHandler(int number) {
@dulimarta
dulimarta / main.rs
Last active February 6, 2020 03:18
CS452 Lab05 - Sample 1 (Rust)
use core::ffi::c_void; // FFI: Foreign Function Interface
use libc::{c_int, shmid_ds};
use libc::{shmat, shmctl, shmdt, shmget};
use libc::{IPC_PRIVATE, IPC_RMID, S_IRUSR, S_IWUSR};
const FOO: usize = 4096;
fn main() {
println!("Hello, world!");
let shm_ptr: *mut c_void;
@dulimarta
dulimarta / sample3.rs
Created January 28, 2020 21:34
CS452 Lab04 - Sample 3 (Rust)
use nix::unistd::sleep;
use std::thread;
// Global variable
static mut SHARED_DATA: u8 = 5;
fn main() {
// Use lambda when the thread function takes arguments
let thr1 = thread::spawn(|| do_greeting3('a'));
let thr2 = thread::spawn(|| do_greeting3('b'));
@dulimarta
dulimarta / lab04_c.cpp
Created January 28, 2020 21:11
CS452 Lab04 - Sample 3 (C++)
#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
void do_greeting3(char arg);
//arguments:arg is an untyped pointer pointing to a character
// returns:a pointer to NULL
// side effects:prints a greeting
@dulimarta
dulimarta / sample2.rs
Last active January 28, 2020 04:54
CS452 Lab04 - Sample 2 (Rust)
// extern crate rand;
// use nix::unistd::sleep;
// use std::process;
use rand::Rng;
use std::thread;
fn main() {
// Use lambda when the thread function takes arguments
let thr1 = thread::spawn(|| do_greeting2(30));
let thr2 = thread::spawn(|| do_greeting2(50));
@dulimarta
dulimarta / lab04_b.cpp
Last active January 30, 2020 17:54
CS452 Lab04 - Sample 2 (C++)
// Compile with -lpthread flag
#include <thread>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
/* Thread functions in C++ can take any number of args of any type */
void do_greeting2 (int, string);
@dulimarta
dulimarta / lab04_b.c
Created January 28, 2020 03:22
CS452 Lab04 - Sample 2
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* do_greeting2 (void* arg);
@dulimarta
dulimarta / lab04_a.cpp
Last active January 28, 2020 02:42
Lab04 - Sample 1 (C++)
// Compile with -lpthread flag
#include <iostream>
#include <thread>
#include <unistd.h>
using namespace std;
void do_greeting();
int main() {