LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-21-2013, 12:21 AM   #1
chaiein
LQ Newbie
 
Registered: Jul 2013
Posts: 1

Rep: Reputation: Disabled
how to use system call sys_sync(() in a c program and why is it used?


I need a sample program that shows the working of sys_sync.
 
Old 07-21-2013, 10:17 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Are you talking about committing the buffer cache to disk, as when you would simply type
Code:
sync
in a terminal window?

If so, you would more likely want to use either the sync() function or the syncfs() function (see the manual page, man 2 sync, for details):
Quote:
DESCRIPTION
sync() causes all buffered modifications to file metadata and data to be written to
the underlying file systems.

syncfs() is like sync(), but synchronizes just the file system containing file
referred to by the open file descriptor fd.
Both functions are part of glibc, the sync() function is pretty much universal (it's found in UNIX and Linux systems), the syncfs() function is Linux-specific.

I would think that calling sync() would probably be your better choice; it's usually a good idea to use the library functions rather than the low-level functions/macros such as sys_sync because the library functions are written to invoke the low-level call with appropriate arguments and you'll usually be more successful that way.

Simply typing sync at a command prompt you'll probably notice that it takes a while to execute (the system buffers are being written to disk). I would write a little C program that simply calls sync() and see if it either takes a while or returns immediately -- if it takes a while, you're good to go but if the program exits immediately that means that the sync() is forked and exec'd and you would want to include a few seconds of sleep after the call to allow the buffers to be flushed to disk(s).

Something like this will give you an idea
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

void	main	(int argc, char *argv [])
{
	time_t	t0 = (time_t) 0;	/* start time			*/
	time_t	t1 = (time_t) 0;	/* finish time			*/

	/*	get a start time				*/
	if (time (&t0) < (time_t) 0)
		(void) fprintf (stderr,
		    "%s:\tcan't read system clock\n", argv [0]);
	/*	call the sync() library function	*/
	sync();
	/*	get a finish time			*/
	if (time (&t1) < (time_t) 0)
		(void) fprintf (stderr,
		    "%s:\tcan't read system clock\n", argv [0]);
	(void) fprintf (stderr,
	    "%s duration %g seconds\n",
	    argv [0], difftime (t1, t0));
	exit (EXIT_SUCCESS);
}
Hope this helps some.
 
Old 07-22-2013, 09:38 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
An immediate return from sync can also be that there is nothing to sync.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Open a file from a c program using system call. abhishekgit Linux - Newbie 8 01-27-2012 10:10 PM
A question about the system call mount in a C program dariyoosh Programming 2 08-16-2009 05:41 PM
issue system call from running fortran program msander Linux - Newbie 1 04-24-2009 07:49 AM
HELP:Error running program which includes system call stefio Programming 2 07-27-2008 10:37 AM
How to issue system call from running fortran program swerdna Programming 3 12-27-2006 07:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration