Linux shared memory chat room

Source: Internet
Author: User
Linux shared memory chatting room shared memory is very effective and fast, suitable for transferring large data between processes. However, the shared memory will not be released with the termination of the program. it must be released in the shmctl shared memory area. Otherwise, it will remain in the system memory area and affect the running efficiency. This practice...
Linux shared memory chatting room shared memory is very effective and fast, suitable for transferring large data between processes. However, the shared memory will not be released with the termination of the program. it must be released in the shmctl shared memory area. Otherwise, it will remain in the system memory area and affect the running efficiency. In this practice, to implement the functions of the chat room, the method of setting up a single display at the beginning to overwrite the shared memory is found to be difficult to control the output dead loop, lack of blocking, and then switched to array storage, the shared memory is divided into N subintervals, which are difficult to control when multiple terminals read and write together. Www.2cto.com # include # Include # Include # Include # Include # Include # Include # Include # Include Www.2cto.com # define KEY 123456 # define SIZE 10240 # define MYKEY 1346766 # define IKEY 13467669 int main () {int shmid; char * shmname; // defines the shared memory int shmid2 of the input name; char * shmstr; // defines the shared memory of the input statement www.2cto.com int j = 0; int I = 0; char temp [100]; shmid = shmget (IKEY, SIZE, IPC_CREAT | 0600); if (shmname = (char *) shmat (shmid, 0, 0); // connection sharing coexistence if (shmid =-1) printf ("shmid error"); shmid2 = shmget (MYKEY, SIZE, IPC_CR EAT | 0600); shmstr = (char *) shmat (shmid2, 0, 0); if (shmid2 =-1) www.2cto.com printf ("shmid2 error "); memset (shmname, 0, SIZE); memset (shmstr, 0, SIZE); char myname [10]; printf ("Enter your name :"); scanf ("% s", myname); // enter the local username printf (": % s has successfully logged on to \ n", myname); pid_t id; www.2cto.com if (id = fork () =-1) printf ("fork error"); // printf ("here 1"); if (id = 0) // the sub-process is used to output {while (1) {if (* (shmstr + j * 100 )! = 0) {printf ("% d sentence", j); printf ("% s said:", shmname); printf ("% s ", (shmstr + j * 100); j ++; www.2cto.com printf ("\ n"); // break; // sleep (1 );}}} else if (id> 0) {while (1) {scanf ("% s", temp); // storage temporary statement for (I = 0; I <1000; I ++) {// printf ("% d unit", I); www.2cto.com if (* (shmstr + 100 * I) = 0) {strcpy (shmname, myname); // merge the user name of the current process to strcpy (shmstr + 100 * I, temp); break; www.2cto.com // printf ("666666666666666666666 \ n") ;}}} shmdt (shmname); // Detach the shared memory from the current process shmctl (shmid, IPC_RMID, NULL ); shmdt (shmstr); // separate the shared memory from the current process. shmctl (shmid2, IPC_RMID, NULL); return 0;} author: Dongfang.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.