Skip to content

Instantly share code, notes, and snippets.

@josephok
josephok / SassMeister-input-HTML.html
Created April 15, 2014 01:26
Generated by SassMeister.com.
<section id="parent">
<section id="child">
</section>
</section>
@josephok
josephok / euler107.py
Last active August 29, 2015 14:01
Prim algorithm in python to solve Euler Problem 107
from collections import defaultdict
# 构造图
# 顶点由'1', '2', '3', ... '40'构成
def build_gragh():
graph = dict()
with open('network.txt') as f:
lines = f.read().splitlines()
for vh, line in enumerate(lines, start=1):
g = dict()
@josephok
josephok / factors.py
Created May 11, 2014 04:57
Factors of an integer
from collections import Counter
def factors(n):
f = []
i = 2
while n > 1:
while n % i == 0:
n /= i
f.append(i)
i += 1
@josephok
josephok / 3.6_1.c
Created October 11, 2014 13:28
APUE Exercises 3.6
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int fd, i;
@josephok
josephok / mydup2.c
Created October 27, 2014 04:27
mydup2.c
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <linux/limits.h>
#define OPEN_MAX 1024
int mydup2(int filedes, int filedes2)
@josephok
josephok / Exercises4.1
Last active January 29, 2023 00:00
The Linux Programming Interface Exercise 4-1
The tee command reads its standard input until end-of-file, writing a copy of the input
to standard output and to the file named in its command-line argument. (We show
an example of the use of this command when we discuss FIFOs in Section 44.7.)
Implement tee using I/O system calls. By default, tee overwrites any existing file with
the given name. Implement the –a command-line option (tee –a file), which causes tee
to append text to the end of a file if it already exists. (Refer to Appendix B for a
description of the getopt() function, which can be used to parse command-line
options.)
@josephok
josephok / 5-2
Created January 5, 2015 13:27
The Linux Programming Interface Exercise 5-2
Write a program that opens an existing file for writing with the O_APPEND flag, and
then seeks to the beginning of the file before writing some data. Where does the
data appear in the file? Why?
It appears at the end. Cause you set O_APPEND, every write will append at the end of the file.
@josephok
josephok / 5-6
Created January 5, 2015 14:06
The Linux Programming Interface Exercise 5-6
After each of the calls to write() in the following code, explain what the content of
the output file would be, and why:
fd1 = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
fd2 = dup(fd1);
fd3 = open(file, O_RDWR);
write(fd1, "Hello,", 6);
write(fd2, "world", 6);
lseek(fd2, 0, SEEK_SET);
write(fd1, "HELLO,", 6);
write(fd3, "Gidday", 6);
@josephok
josephok / 5-5
Created January 5, 2015 14:43
The Linux Programming Interface Exercise 5-5
Write a program to verify that duplicated file descriptors share a file offset value
and open file status flags.
@josephok
josephok / 26_1.c
Created January 17, 2015 01:40
Write a program to verify that when a child’s parent terminates, a call to getppid() returns 1 (the process ID of init).
// Write a program to verify that when a child’s parent terminates, a call to getppid()
// returns 1 (the process ID of init).
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
pid_t fork(void);