From 78b57e0eb38aaedfd65ab94ef53ea85b6a234167 Mon Sep 17 00:00:00 2001 From: PrismRealm Date: Sun, 17 Mar 2019 23:28:35 +0800 Subject: [PATCH] Initial commit --- Problem 3.16/SocketClient.class | Bin 0 -> 1040 bytes Problem 3.16/SocketClient.java | 24 +++++++++++++++ Problem 3.16/SocketServer.class | Bin 0 -> 1383 bytes Problem 3.16/SocketServer.java | 36 ++++++++++++++++++++++ Problem 3.18/SocketClient.class | Bin 0 -> 1619 bytes Problem 3.18/SocketClient.java | 35 +++++++++++++++++++++ Problem 3.18/SocketServer.class | Bin 0 -> 1560 bytes Problem 3.18/SocketServer.java | 37 +++++++++++++++++++++++ Problem 3.20/main.c | 52 ++++++++++++++++++++++++++++++++ README.md | 4 +++ 10 files changed, 188 insertions(+) create mode 100644 Problem 3.16/SocketClient.class create mode 100644 Problem 3.16/SocketClient.java create mode 100644 Problem 3.16/SocketServer.class create mode 100644 Problem 3.16/SocketServer.java create mode 100644 Problem 3.18/SocketClient.class create mode 100644 Problem 3.18/SocketClient.java create mode 100644 Problem 3.18/SocketServer.class create mode 100644 Problem 3.18/SocketServer.java create mode 100644 Problem 3.20/main.c create mode 100644 README.md diff --git a/Problem 3.16/SocketClient.class b/Problem 3.16/SocketClient.class new file mode 100644 index 0000000000000000000000000000000000000000..0617585709e6dcd556120aaec703fac65979f8b8 GIT binary patch literal 1040 zcmZuwT~8B16g^Y6opxD13PnIgrGAyq0-{xlfFQ*t1&lQ&jZaHEkcD=)DO;nD{u-Xa zq)jC8(FcEwzrcX^ZlScwPI71V-ZS@}IdkXN+4mm+=8@LXhp5E;ARg%O;USwx5|1Tf zIwZz)1TfBKB7{jy>6k{GF*6dg?3rWF6N!0=rviaR%eLGlfsWYN2LWx(DJX%SjAg5> zYH3GR-kUo`@@OT~vITC$K4$jKPiDMm+I#V=Te0lDBya4>x@P`h(=2;U1F%rQX|IO7umctu!Loa6$7uZDzRqZHP#KJ zm_UDX|MvRlyehkvV;k7ODudn#7_H0&dOf(O#`ey>%Db%@t{Aid8?q7b8ZBAk{}oQN z#NB(!Z8?;{U~H@bB^g=*q|VSqj(-B2rT;3_kj1;cAjwqVOBaY-W^KLVcK%-uT~(r% zajJCOfhLEy)D0Jk6NjddEp7o>W>Aq@Y2cc3GC_a7=o}J{4ppJ+Y(q5^Cc+p+n4STD zK2Rw{$PUoo2+e|K|9BnZm=>fb*?{NN5W*maXwA@X@Y4V`PT|Y-b%Y1M)}du4YVdEK zB9J?U%uUu2tRu8FRfC>e(0-!(1f9cL4aU*=Z$fq*p)+%W9$NMudnJ<8S>42mi;hDl z8$=hC?W1*+B9rJrg8q_(SR&TTL?uO}h6x#kcE&IJro=6Y+kX&|7&&L|5_j0#l~m&% IA$=JA3p&Z{F#rGn literal 0 HcmV?d00001 diff --git a/Problem 3.16/SocketClient.java b/Problem 3.16/SocketClient.java new file mode 100644 index 0000000..7f89636 --- /dev/null +++ b/Problem 3.16/SocketClient.java @@ -0,0 +1,24 @@ +import java.net.*; +import java.io.*; + +public class SocketClient { + public static void main(String[] args) { + try { + // make connection to server socket + Socket sock = new Socket("127.0.0.1", 6017); + InputStream in = sock.getInputStream(); + BufferedReader bin = new BufferedReader(new InputStreamReader(in)); + + // get a qoute from the socket + String line; + while ( (line = bin.readLine()) != null) + System.out.println(line); + + // close the socket connection + sock.close(); + } + catch (IOException ioe) { + System.err.println(ioe); + } + } +} \ No newline at end of file diff --git a/Problem 3.16/SocketServer.class b/Problem 3.16/SocketServer.class new file mode 100644 index 0000000000000000000000000000000000000000..4cbe6363eebde51c7ed4e10c6e529a5b75ca33f1 GIT binary patch literal 1383 zcmZuxU31$+6g`S%%P|f~T+*}+luZl7N!=LQLZ}@IB>fA_Z@st!W34_ zP<60c!Wu4>kVkDAZ{f0;ts7h2!H3iM2v3Q4}E>ZLaw^{z=FLUb(DqKT|ZR!(tcaT_hmbv z#qLW#v~armKUqxRjH{*ByDJA7o=rGdBtr|uO)q#=qU1(t?5W$nNjUFDUQcOP#gA0H zZkUyE24^iSWGq9a>m%O?XPlpvaRZ;TlMLZU^>5jP{x0^liXD7W#+TSKxa~4dW3h~z zxK+k&lXD!gvv=zkPYtvmg%0kN@fCI)d~KlLSeQGxKNdD_M&XJUUOW_S<;gTr!q+Q_ zh)B97l0I9RaEPuP3{lSKTN(|X=qJsUVP{a?TPze>76p4^QtYl{>wCt&n@E`9# zj)d;=Q0R*Z3HQyesRoToq@m9QjFe$Vbkn{JMWWMz-|_XZnHhIh`XY?9NKyvNHqZQ6 zAS;}ABf&s65=Fg)GJf2R4C{=7sg=kfZBl2(eWilhxLw_iNXP{A zeIh&AM)jrUVbn586M)S68bUOyl_$^t*xv88A1Y5%a@8H9tb+8+iSZDtt0M(x+Q`&H z0nh#?m&`=Vfzo@a9;BKoSF&&6%U_H%%OB@TC(Pq%w8N@)Jz~&1AZ09zgW2!a3`0EjJcWZ~R?>$Gpb@>nl z`v^|MK5IWiap}q-rkCuWIDIkLq_gzo0JlDwdDk*H>JkbJ8cC>|yT!H;e{f!yR!MiU}1OLJd%P8W)xWDTpZNBB=A9%Bs ltIWDByE5mt@|AhFRj5?lmQy+9{)Xbo-t(k*k%3cq?_V&NR(=2g literal 0 HcmV?d00001 diff --git a/Problem 3.16/SocketServer.java b/Problem 3.16/SocketServer.java new file mode 100644 index 0000000..ffeeab3 --- /dev/null +++ b/Problem 3.16/SocketServer.java @@ -0,0 +1,36 @@ +import java.net.*; +import java.io.*; + +public class SocketServer { + public static String[] qoutes = { + "Don't cry because it's over, smile because it happened.", + "Be yourself; everyone else is already taken.", + "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.", + "So many books, so little time.", + "Be who you are and say what you feel, because those who mind don't matter, and those who matter don't mind." + }; + + public static void main(String[] args) { + try { + int i = 0; + ServerSocket sock = new ServerSocket(6017); + + // Listening for connections + while (true) { + Socket client = sock.accept(); + PrintWriter pout = new PrintWriter(client.getOutputStream(), true); + + // write a qoute to the socket + pout.println(qoutes[i]); + i = (i + 1) % qoutes.length; + + // close the socket and resume + // listening for connections + client.close(); + } + } + catch (IOException ioe) { + System.err.println(ioe); + } + } +} diff --git a/Problem 3.18/SocketClient.class b/Problem 3.18/SocketClient.class new file mode 100644 index 0000000000000000000000000000000000000000..d7e35fc82fe01d99bb9f4ccccc3df78d854f62c9 GIT binary patch literal 1619 zcmZuxU2hvj6g?ArJ8HaT3Vd8K+^;aj#KIsFs0CqJxw-ob}eU} zl$T2IPzj`{4?I*tNbr{D2FaF)1QL(D@Q>=AS?6O*;>NKhmR*H83mji4Oc!PjWhHRx{2@M5ny6 z66nr3uFBU}7gTNDUZ~I#U9}xoU@&zjw`{N5>5AHNZ>U zYiIdT5d z_&Gik8gh%V0i^Cbm2{zGyRNE96fDf+wg#}AV*$YN(l>11o~!%Sy3b;jy=vhLe5pZ2 zfuS%c>xM92;f{r`aaTYF_RNUHHx|CdJqtD#ER=9>FFvHi^}16js~T0EE-iVP5esD~ z3-@)zqJAyuu)e*t*}3TlB~|qu&$Zy-uJ$imSkWR_VRZz0f|wxH+`_Ud`J_3d6MN(V zaDJ)gZM3^Ln0mQuS>3h}Z{fQ&U#$vs=spj$@f0wm%OrK(CHkL3Q*|#YKdiI{kM7fi zJ>*Fq@VwyBL`O*!%1WI>JRt{ZB3_+J929$#`*F9?^zI8CG7A`04Pw+Gn&|X#S3U6c z{uk)qOC0JkdE*;3$5*%KXV_@FT2*dYVC*oqmqg#)AmotTjOBnL?tATXL}O4|h5AQS zjoavj65D+b4x3S`cx#HA_*LE{`qu%$*(ttX=jY{Ja%;D{buO>||V+c=u& zZlLFE1HBnzENL{5plt_z#U}a_0}Tu&qYVu4+rY5~l3T6+GR8Ju(Z~~9t$*?yk8wzX zlVly{KEMrM|$27 zta_OiKynsmq={Z$k0)Rug)TDo;3_RsNFd7_^F9Wdp`Ryk0BantBZ-Y5(=dLZ*pDdp zPZWBS5Fn;+#p%dnRIdU*ZFO`OriHA8AQ`1Pizz@v(_fbRgOB}xME3y$ab7X_l(xitY}v0haI$)e+_ z<@U~oYOdNFE+xi}?RWxH*}KIp`@WrXZLgLq`%TBIEoj5hvTs+mOLil`ODr=zi52F> zY#5q=S+2L66}9AOx>UJd*;am8HSeqDS{+QKYz@^*Z9W5x7v&mZ=hnKii*I*&|6{U>JJsw@SVD6LE#Q7=e1o|V$(tm z-&t@lYherbwDq=t6)uFpSilp+u(GkGDn6-SQE_%C00wmmDoH=tf^J0Iu4w=r~ z92ydY8bISvLCOzXzS`lW*4s>PazD769Kyj2&>(I%8p^8*%n#g#`35nDM1vmFFyYtx z^Bn4Ch?=oBv?G_VVDeSe%qH=m>G�$7F>{s@1Yif3swqD}u_k3ZF zV-u^m^%iZTs>um_!Q&9MhFQGMPl^6TAoK&``UY<^+y(d8sV>A*9t4hajR!&lBX|>U z@l2?AfGFj_@1iJ5dx)&3rXO_?EuQYc;61i}rh|AFa(NF4eIDt;%p3C^9LUF>!P>*< zdKU-BlO2p@Vq7{%bugYW^krhV_fI~iPm|+Q9ZWw#e3$>;Uo=l=j1Dp+JWNkVc9CAz zyT0!tmN%Z^HG*cIGP-%J;x@l*27bg(+#lgFe&HSoa@s^D$SeX$#0ZkmU%>+`hDFOH z&S4BCq{uNvmT^A$32YMK@a`gw2JPE8jE6Wv{^Rs8Lk~ysGZ`MUpue(8zp-8?=;a0y vJ(9*GPD*4Y<|Ix@oCX|;GpR_A)X{w%1cg0DV(g132&CfQpcV6^jNt759QtMu literal 0 HcmV?d00001 diff --git a/Problem 3.18/SocketServer.java b/Problem 3.18/SocketServer.java new file mode 100644 index 0000000..d359208 --- /dev/null +++ b/Problem 3.18/SocketServer.java @@ -0,0 +1,37 @@ +import java.net.*; +import java.io.*; + +public class SocketServer { + public static void main(String[] args) { + ServerSocket sock = null; + Socket client = null; + try { + sock = new ServerSocket(6017); + client = sock.accept(); + DataInputStream inputStream = new DataInputStream(client.getInputStream()); + DataOutputStream outputStream = new DataOutputStream(client.getOutputStream()); + while (true) { + try { + String message = inputStream.readUTF(); + System.out.println("Client: " + message); + outputStream.writeUTF(message); + } catch (IOException e) { + if (e.getMessage().equals("null")) { + break; + } + } + } + inputStream.close(); + outputStream.close(); + client.close(); + sock.close(); + } + catch (IOException ioe) { + try { + sock.close(); + client.close(); + } catch (IOException e) {} + System.err.println(ioe); + } + } +} diff --git a/Problem 3.20/main.c b/Problem 3.20/main.c new file mode 100644 index 0000000..c26238c --- /dev/null +++ b/Problem 3.20/main.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#define SIZE 65536 + +int main(int argc, char *argv[]) { + int pipefd[2]; // pipefd[0] represents input stream id, pipefd[1] represents output stream id. + char buffer[SIZE]; // The buffer of main process. + char childBuffer[SIZE]; // The buffer of sub-process. + + // If the number of arguments is not correct. + if (argc != 3) { + perror("Main: main [target] [destination].\n"); + return 1; + } + + char *srcFile = argv[1]; // Source + char *dstFile = argv[2]; // Destination + + // Building a pipe. If the returned integer is less than 0, + if (pipe(pipefd) < 0) { + printf("An error occured when createing the pipe: %s\n", strerror(errno)); + return 1; + } + + pid_t pid = fork(); // Create a sub-process. + + if (pid == 0) { // pid == 0 represents it's a sub-process. + close(pipefd[1]); // Close pipefd[1] + ssize_t file_size = read(pipefd[0], childBuffer, sizeof(childBuffer)); // Get the size of child + close(pipefd[0]); // Close pipefd[0] since it is unable to use. + int dst_file_fd = open(dstFile, O_CREAT | O_WRONLY); // Read a file in writeonly mode. If the file does not exit, create a new one. + write(dst_file_fd, childBuffer, file_size); // Write data from buffer into fd. + close(dst_file_fd); // Close file + } + else if (pid > 0) { // pid > 0 represents it's a main process. + close(pipefd[0]); // Close pipefd[0] + int target_file_fd = open(srcFile, O_RDONLY); // Open a file in readonly mode. + ssize_t file_size = read(target_file_fd, buffer, sizeof(buffer)); // Get size of the file + write(pipefd[1], buffer, file_size); // Write data from buffer into pipefd[1] + close(pipefd[1]); // Close pipefd[1] + close(target_file_fd); // Close file + } + else { // When error occur + printf("An error occured when forking child process. %s\n", strerror(errno)); + return 1; + } + return 0; +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..9edacb9 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Operating System Second Assignment +=== + +Programming Problems 3.16, 3.18, 3.20 對應至目錄 `Problem 3.16`, `Problem 3.18` 與 `Problem 3.20` 上