Use fcntl () to set the blocking nature of the blocking function. The fcntl Function

Source: Internet
Author: User

Use fcntl () to set the blocking nature of the blocking function. The fcntl Function

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#define ERR_EXIT(m) \    do { \        perror(m);\        exit(EXIT_FAILURE);\    }while(0)void activate_nonblock(int fd);void deactivate_nonblock(int fd);int main(int argc, const char *argv[]){    activate_nonblock(STDIN_FILENO);    char buffer[1024] = {0};    int ret = read(STDIN_FILENO, buffer, 1024);    if(ret == -1)        printf("read");    return 0;}void activate_nonblock(int fd){    int ret;    int flags = fcntl(fd, F_GETFL);    if(flags == -1)        ERR_EXIT("fcntl");    flags |= O_NONBLOCK;    ret = fcntl(fd, F_SETFL, flags);    if(ret == -1)        ERR_EXIT("fcntl");}void deactivate_nonblock(int fd){    int ret;    int flags = fcntl(fd, F_GETFL);    if(flags == -1)        ERR_EXIT("fcntl");    flags &=~O_NONBLOCK;    ret = fcntl(fd, F_SETFL, flags);    if(ret == -1)        ERR_EXIT("fcntl");}


What are the differences between the blocking and non-blocking modes when using Socket?

The main difference between the blocking mode and the non-blocking mode is that when there is no request, the blocking mode will stop at the receiving function, that is, the accep function, and will continue to process down until a request arrives. In non-blocking mode, the system runs the receiving function. If a request exists, the system receives the request. If no request exists, a negative value is returned and the system continues to run the function downward.
In general, there are many programs that use the blocking mode, because the blocking mode is supported by the kernel to wait for requests. When the blocking mode is blocked, it does not occupy system resources, but the non-blocking mode requires manual polling, A large amount of resources are occupied. In addition, you can use the select function to set the timeout value in blocking mode. For more information, see related books.

What is a blocking operation?

Blocking operation

A blocking operation means that when a device operation fails to obtain the resource, the process suspends until the operation meets the operational conditions. A non-blocking process does not stop when it cannot perform device operations. The suspended process enters the sleep state and is removed from the running queue of the scheduler until the waiting conditions are met.

In the Linux driver, we can use the wait queue to implement blocking operations. Wait queue appeared in the Linux kernel as a basic function unit for a long time. It uses the queue as the basic data structure and is closely integrated with the process scheduling mechanism, it can be used to implement the core asynchronous event notification mechanism. Wait queue can be used to synchronize access to system resources.

The following defines the device "globalvar", which can be opened by multiple processes. However, this process or other processes can read the data only after a process writes data, otherwise, it will be blocked.

# Include <linux/module. h> # include <linux/init. h> # include <linux/fs. h> # include <asm/uaccess. h> # include <linux/wait. h> # include <asm/semaphore. h> MODULE_LICENSE ("GPL"); # define MAJOR_NUM 254 static ssize_t globalvar_read (struct file *, char *, size_t, loff_t *); static ssize_t globalvar_write (struct file *, const char *, size_t, loff_t *); struct file_operations globalvar_fops = {read: globalvar_read, write: globalvar_write,}; static int global_var = 0; static struct semaphore sem; static writable outq; static int flag = 0; static int _ init globalvar_init (void) {int ret; ret = register_chrdev (MAJOR_NUM, "globalvar", & globalvar_fops); if (ret) {printk ("globalvar register failure");} else {printk ("globalvar register success"); init_MUTEX (& sem); init_waitqueue_head (& outq);} return ret ;} static void _ exit globalvar_exit (void) {int ret; ret = unregister_chrdev (MAJOR_NUM, "globalvar"); if (ret) {printk ("globalvar unregister failure ");} else {printk ("globalvar unregister success") ;}} static ssize_t globa ...... remaining full text>

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.