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 / 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() {
@dulimarta
dulimarta / sample1a.rs
Last active January 27, 2020 02:21
CS452 Lab04 - Sample 1 (Rust)
use nix::unistd::sleep;
use std::process;
use std::thread;
fn main() {
println!(
"From PID {} thread id {:?}",
process::id(),
thread::current().id()
);
@dulimarta
dulimarta / sample1.rs
Last active September 23, 2020 20:12
CS452 Lab03 - Sample1 (Rust)
extern crate signal_hook;
use std::error::Error;
use std::time::Duration;
use nix::unistd::{pause, sleep};
use signal_hook::{register, SIGINT};
static mut I_CAN_RUN: bool = true;
fn main() -> Result<(), Box<dyn Error>> {
@dulimarta
dulimarta / sample2.rs
Last active September 24, 2020 03:17
CS452 Lab03 - sample 3 (Rust)
use nix::unistd::{close, dup2, fork, pipe, ForkResult};
use std::io::{self, Read, Write};
use std::process::{exit, id};
use std::str;
use std::error::Error;
const MAX: usize = 1024;
fn main() -> Result<(),Box<dyn Error>> {
@dulimarta
dulimarta / sqrt-error.c
Created January 16, 2020 02:17
CS452 Sample - Error checking
// Be sure to link this program with the math library (-lm)
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
double x, z;
printf ("Enter a number: ");
scanf ("%lf", &x);
@dulimarta
dulimarta / lab02_a.rs
Last active January 25, 2024 01:28
CS452 Lab02 - Sample 1 (Rust)
use nix::unistd::fork;
fn main() {
println!("Before fork()");
fork();
println!("After fork()");
}