Thread: fprintf _lock_file EnterCriticalSection

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    fprintf _lock_file EnterCriticalSection

    hello. my program hangs inside an fprintf. debugger follows it to procedure _lock_file where it never escapes from. i believe the final executed command is EnterCriticalSection( &(((_FILEX *)pf)->lock) );

    anyone know why this is happening? the filepath associated with the file pointer seems valid and i didnt get any error messages opening the file. thank you for your time.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you didn't mention which OS/Compiler you're using.
    Nor did you post a simple program which demonstrates the fault.
    Nor did you mention for example whether you were using threads in your program.

    Two scenarios,
    1. Your code is full of bugs, and it's managed to trash the library data structures into thinking there is a file lock, when there isn't.
    2. You've mixed thread-safe and non thread-safe code in a threaded program, and the collective mis-understanding is what you're seeing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fprintf is thread safe, so it enters a critical section whenever you try to write to a file.
    So if another thread is trying to write to the same file and somehow hangs/fails/takes time, your thread will be blocked until the other thread is finished.
    So as Salem mentions, show some code to illustrate the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    thank you for your time. no threads here so i understand therefor it must be a bug as you say. i'm bubblesorting arrays of structs of files pointers numerous times so i must have damaged a file pointer somewhere. you've helped me and i appreciate it. thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 06:58 PM