network programming重点回顾

#1 

----Inter-Process Communications (Pipes, Sockets, Signals, etc.)

Piping is a process where the output of one process is made the input of another. We have seen examples of this from the UNIX command line using | . 

We will now see how we do this from C programs. We will have two (or more) forked processes and will communicate between them. 

We must first open a pipe .UNIX allows two ways of opening a pipe.

①popen() -- Formatted Piping

FILE *popen(char *command, char *type)

-- opens a pipe for I/O where the command is the process that will be connected to the calling process thus creating the pipe. The type is either "r" - for reading, or "w" for writing. 
popen() returns is a stream pointer or NULL for any errors. 
A pipe opened by popen() should always be closed by pclose(FILE *stream). 
We use fprintf() and fscanf() to communicate with the pipe's stream.
②pipe() -- Low level Piping

int pipe(int fd[2]) 

-- creates a pipe and returns two file descriptors, fd[0], fd[1]. fd[0] is opened for reading, fd[1] for writing. 
pipe() returns 0 on success, -1 on failure and sets errno accordingly. 
The standard programming model is that after the pipe has been set up, two (or more) cooperative processes will be created by a fork and data will be passed using read() and write(). 
Pipes opened with pipe() should be closed with close(int fd). 

pipe:

https://users.cs.cf.ac.uk/Dave.Marshall/C/node23.html

socket:

http://www.it.uom.gr/teaching/distrubutedSite/dsIdaLiu/labs/lab2_1/sockets.html

signals:

https://compas.cs.stonybrook.edu/~nhonarmand/courses/fa14/cse506.2/slides/ipc.pdf




#2 Files, file descriptors, currency indicators

File descriptor is an integer in your application that refers to the file description in the kernel.

File description is the structure in the kernel that maintains the state of an open file (its current position, blocking/non-blocking, etc.). In Linux file descripion is struct file.


Ordinary files permit read or write operations at any position within the file. Some other kinds of files may also permit this. Files which do permit this are sometimes referred to as random-access files. You can change the file position using the fseek function on a stream (see section File Positioning) or the lseek function on a file descriptor (see section Input and Output Primitives). If you try to change the file position on a file that doesn't support random access, you get the ESPIPE error.


#3 Processes, threads, process creation

process: Process (slightly oversimplified): "An execution stream in the context of a particular process state."
Execution stream: a sequence of instructions (only one thing happens at a time).
Process state: everything that can affect, or be affected by, the process: code, data, call stack, open files, network connections, etc.

int pid = fork();
if (pid == 0) {
    /* Child process  */
    exec("foo");
} else {
    /* Parent process */
    waitpid(pid, &status, options);
}


thread: multiple execution streams within a single process
Threads share process state such as memory, open files, etc.
Each thread has a separate stack for procedure calls (in shared memory)
Thread is unit of sequential execution


(stanford cs140 os)


#4 Memory allocation


Using C malloc() and free()

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, i, *ptr, sum = 0;    //pointer 

    printf("Enter number of elements: ");
    scanf("%d", &num);

    ptr = (int*) malloc(num * sizeof(int));  //memory allocated using malloc
    if(ptr == NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }

    printf("Enter elements of array: ");
    for(i = 0; i < num; ++i)
    {
        scanf("%d", ptr + i);
        sum += *(ptr + i);
    }

    printf("Sum = %d", sum);
    free(ptr);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, i, *ptr, sum = 0;
    printf("Enter number of elements: ");
    scanf("%d", &num);

    ptr = (int*) calloc(num, sizeof(int));
    if(ptr == NULL)
    {
        printf("Error! memory not allocated.");
        exit(0);
    }

    printf("Enter elements of array: ");
    for(i = 0; i < num; ++i)
    {
        scanf("%d", ptr + i);
        sum += *(ptr + i);
    }

    printf("Sum = %d", sum);
    free(ptr);
    return 0;
}

Using C calloc() and free()


https://www.programiz.com/c-programming/c-dynamic-memory-allocation


#5 General C programming topics (arrays, loops, etc.)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值