Skip to content

Instantly share code, notes, and snippets.

View melouver's full-sized avatar

Yuzhe (Lucas) Li melouver

View GitHub Profile
@melouver
melouver / rwqueue.c
Created November 26, 2017 02:26
RWQueue solution for reader-writer problem(somewhat fair for each others)
/* Code to implement readers/writers that preserves FIFO priority */
#include "csapp.h"
#include <stdbool.h>
/* Represents individual thread's position in queue */
typedef struct TOK {
bool is_reader;
sem_t enable; // Enables access
struct TOK *next; // Allows chaining as linked list
@melouver
melouver / second.c
Last active November 26, 2017 02:37
second reader-writer problem solution(second means writer has high priority)
/*
COPYRIGHT: CSAPP author RB
*/
/* My thoughts
For the second rw problem, we give the writers have higher priority
than readers.That is when a writer arrives at some time, it will wait
all the readers in critical section currently to finish their work,
and at the same time, prevent any incoming readers entering their critical
section ,by doing a P operation on r.
@melouver
melouver / first.c
Created November 26, 2017 02:23
Firstreader-writer problem solution (first: reader with high priority )
/*
COPYRIGHT: CSAPP author RB
*/
#define NITERS 100
/* Sample code demonstrating reader-writers. Create NITERS agents,
* numbered from 1 to NITERS. Each agent is randomly chosen to bea
* reader (probability 80%) or a writer (probability 20%). Writers
* assign their ID to the global value. Readers read the global
@melouver
melouver / cmd.sh
Last active November 22, 2017 02:43
GDB command in mind
thread apply all bt
用于打印所有线程调用栈,debug死锁
x/bx ptr
以字节为单位打印内存
@melouver
melouver / inversion_pairs.cpp
Last active November 21, 2017 14:35
just add sth to merge sort and we can get inversion pairs
int cnt = 0;
void merge(vector<int> &A, int p, int q, int r)
{
int n1 = q - p + 1; // # of L
int n2 = r - q; // # of R
vector<int> L(n1+1, 0), R(n2+1, 0);
for (int i = 0; i < n1; i++) {
L[i] = A[p+i];
}
for (int i = 0; i < n2; i++) {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define BIDIRECTIONAL 0 /* change to 1 if you're doing extra credit */
/* and write a routine called B_output */
#define DEBUG 1
#define N 8
/* a "msg" is the data unit passed from layer 5 (teachers code) to layer */
/* 4 (students' code). It contains the data (characters) to be delivered */
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cassert>
using namespace std;
//static const int CNT = 10;
@melouver
melouver / 1029.md
Last active November 25, 2017 15:04
today
@melouver
melouver / malloc-hint2.txt
Created October 28, 2017 11:02 — forked from kowakowa93/malloc-hint2.txt
hints malloc lab
(text of an email to the class from Dave O'Hallaron)
I've noticed that a number of you are having trouble getting started
with Lab 6. The purpose of this note is to help you get started by
guiding you through the initial steps I take when I do the lab.
This lab is too hard to do all at once, so I break it up into more
managable pieces. I start by developing a simple implicit list
allocator. Later, I will improve this code with a faster and more
memory efficient free list organization. But by starting with the
@melouver
melouver / 10.28
Last active November 18, 2017 01:41
gist today
CMU database group:15445
Youtube: https://www.youtube.com/watch?v=xjhQ0e9Hlds&list=PLSE8ODhjZXjYutVzTeAds8xUt1rcmyT7x
course info: [main](http://15445.courses.cs.cmu.edu/fall2017/index.html)
Linux man page:
[manpage](https://linux.die.net/man/)
UIUC system programming